2

I created a code that shows a real time clock at the beginning (works by a loop and refreshing itself in every 1 sec using \r ) But I want to run the rest of the code while the clock is ticking (continuously). But this isn't going any further while the loop is running. I think there is no need to write the code of the clock.

1
  • 3
    You can try multi-threading.. Commented Jun 30, 2019 at 5:21

1 Answer 1

2

If you want to have a task running, while using another you can use multi-threading. This means you tell your processor two different tasks and it will be continued as long as you tell it to work. See here a post about multithreading and multiprocessing. You can use the thread function of python for this.

Here a small example:

import threading
import time
# Define a function for the thread
def print_time( threadName, delay):
   count = 0
   while count < 10:
      time.sleep(delay)
      count += 1
      print ("%s: %s" % ( threadName, time.ctime(time.time()) ))

def counter(threadName, number_of_counts, delay):
    count=0
    while count < number_of_counts:
        print ("%s: %s" % ( threadName, count))
        time.sleep(delay)
        count +=1
# Create two threads as follows
threading.Thread(target=print_time, args=("Thread-1", 1, )).start()
threading.Thread(target=counter, args=("Thread-2", 100, 0.1,)).start()

for further information check the documentation. Note that thread has been renamed to _thread in python 3

Sign up to request clarification or add additional context in comments.

2 Comments

I would recommend using threading.Thread(target=print_time, args=("Thread-1", 1, )).start() rather than using start_new_thread.
@DanD. you are right, threading might be the better solution here. changed it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.