1

i m writing a script in which threads that do simple tasks start every 3 minutes . I m using the threading and schedule modules .

Because of the nature of multi-threading threads are using the same resources .

What i need to achieve ?

When creating a new thread i would like to check if there is any runningthread; and if there is , then wait until the running thread terminates , then start the new thread .

What i have tried ?

 import threading

def run_threaded(job_fn):
    job_thread = threading.Thread(target=job_fn)
    bot.logger.info(" --------------No of active threads : "+threading.activeCount())
    job_thread.start()
    job_thread.join()
    bot.logger.info(" --------------No of active threads : " + threading.activeCount())


schedule.every(3).minutes.do(run_threaded, job)


while True:
    schedule.run_pending()

Note: On the example above every job_thread needs 5 minutes to complete . Thus it creates 1 thread every 6 minutes .

From what i understand , the job_thread.join() line is joining the main thread ( with any other active threads) . Although the schedule , is blocked thus no any other thread can be instantiated before the previous thread is finished . Is that correct ? And if yes , is this a good practice of doing this .?

And for the record .. Can the script execute other blocks of code while running a thread ? Or can it instantiate other threads, before the previous thread is finished , if they are going to execute an other job let's say job2 ?

2
  • 1
    Yes. You can instantiate other threads before the previous thread is finished. In fact, that's really the whole point: If all that your main thread does in each schedule run is: (1) start work thread, (2) wait for work thread to end, then there is no point in creating a work thread at all. You can just do the same work in your main thread. The advantage of multithreading is that different threads can be executing at the same time. If you don't want/need to do multiple things at the same time, you don't need to use multithreading. Commented Sep 12, 2018 at 15:08
  • Ok thanks. Out of curiosity can u give me an example code that instantiate two threads simultaneously? And how can i check which of them is finished and which are still running? Commented Sep 12, 2018 at 15:15

1 Answer 1

1

Here's a simple example showing several threads being started, each of which will terminate itself at a different time and how the main thread can determine when each has terminated.

#!/usr/bin/env python3
import threading
import time
import queue

class Worker(threading.Thread):
    def __init__(self, duration, tqueue):
        self.duration = duration
        self.tqueue = tqueue
        super().__init__()
    def run(self):
        # Do real work here instead of just sleeping
        time.sleep(self.duration)
        # Tell parent we are gone by putting our instance to the queue
        self.tqueue.put(self)

def main():
    thr_duration = [2.0, 1.5, 0.5, 2.7, 1.25]
    workers = []
    thr_queue = queue.Queue()

    # Start all threads
    for dur in thr_duration:
        worker = Worker(dur, thr_queue)
        worker.start()
        workers.append(worker)
        print("Started thread {}, duration {}".format(worker.name, dur))

    # Wait for all threads to terminate
    while workers:
        worker = thr_queue.get()
        worker.join()
        print("Reaped thread {}".format(worker.name))
        workers.remove(worker)

if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.