Search code examples
pythonmultithreadingpython-multithreading

If a separate thread(which uses time.sleep() inside) is made manually in a multi threaded python application, will that affect the performance?


import threading
import time

def scheduler():
    periodic_action() # requires distributed locking and it's enough if it only runs ONCE every 1 hour
    time.sleep(3600) # 1 hour
    threading.Thread(target=scheduler, daemon=True).start()

if __name__ == "__main__":
    threading.Thread(target=scheduler, daemon=True).start()

This code runs in a multithreaded environment with 5 threads (gunicorn gthread). And periodic_action() will do its job, even if it runs only once (that is why I want only one thread to run it). I am not sure is this a good way of scheduling? Will one thread will be always reserved for this scheduling task? and we will be having effectively 4 threads? How does GIL perform for this?


Solution

  • Using time.sleep() inside a thread is a very efficient way of having a thread delay before doing something else. You can have a large number of threads all doing this and it will still be very efficient.

    (However, the periodic_action() will be subject to the GIL, especially if several threads wake up at the same time).

    Additionally, you won't need the scheduler() function start another thread, it may as well be like this:

    def scheduler():
        while True:
            periodic_action()
            time.sleep(3600) # 1 hour