time.sleep -- sleeps thread or process? time.sleep -- sleeps thread or process? python python

time.sleep -- sleeps thread or process?


It blocks the thread. If you look in Modules/timemodule.c in the Python source, you'll see that in the call to floatsleep(), the substantive part of the sleep operation is wrapped in a Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS block, allowing other threads to continue to execute while the current one sleeps. You can also test this with a simple python program:

import timefrom threading import Threadclass worker(Thread):    def run(self):        for x in xrange(0,11):            print x            time.sleep(1)class waiter(Thread):    def run(self):        for x in xrange(100,103):            print x            time.sleep(5)def run():    worker().start()    waiter().start()

Which will print:

>>> thread_test.run()0100>>> 12345101678910102


It will just sleep the thread except in the case where your application has only a single thread, in which case it will sleep the thread and effectively the process as well.

The python documentation on sleep() doesn't specify this however, so I can certainly understand the confusion!

https://docs.python.org/3/library/time.html#time.sleep


matomo