In Python for *nix, does time.sleep()
block the thread or the process?
7 Answers
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 time
from threading import Thread
class 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()
0
100
>>> 1
2
3
4
5
101
6
7
8
9
10
102
-
4How does illustrate a "thread" has got blocked. And why don't only 5 and 103 not get printed, while all other numbers get printed. Would be really helpful for me if someone could explain.– akkiAug 1, 2017 at 1:26
-
@akki: please ask a new question rather than using the comments of an old question. Also, 5 gets printed (it's right before 101). Aug 1, 2017 at 2:32
-
19Open a new question to ask the meaning of this answer? That seems quite weird to me. And I meant 11 (not 5), sorry can't correct my comment now. I actually need some help understanding what point this answer is trying to make.– akkiAug 1, 2017 at 5:55
-
First answer: The range function xrange(k, m) returns the numbers k inclusive up to m-1 inclusive, so the expression list(xrange(100, 103)) returns [100, 101, 102]. This means that length(list(xrange(k, m))) == m - k. Oct 20, 2017 at 14:37
-
3akki, More specifically, time.sleep() blocks the thread that called time.sleep(), but it releases the Python GIL to run other threads (so it doesn't block the process). Nick's example didn't really show the blocking of the thread, it more showed that the GIL is released (thus showing that the process IS NOT blocked). I think if he had more stuff like a print statement after time.sleep(5) in the waiter() thread, it would show that the print didn't happen until after the time.sleep(5) finished (i.e blocking)– gunitDec 1, 2017 at 23:32
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!
-
5@MichaelMrozek:
sleep(3)
says: "sleep() makes the calling thread sleep until seconds seconds have elapsed or a signal arrives which is not ignored." And there is Python documentation bug.– jfsJan 17, 2015 at 4:02
The thread will block, but the process is still alive.
In a single threaded application, this means everything is blocked while you sleep. In a multithreaded application, only the thread you explicitly 'sleep' will block and the other threads still run within the process.
Process is not runnable by itself. In regard to execution, process is just a container for threads. Meaning you can't pause the process at all. It is simply not applicable to process.
-
Huh? This may be true on Windows or something, but certainly not universally. Unix traditionally did not have threads at all, and so a Python program runs a process (with a single thread, in some abstract sense) which is what the
sleep
command will pause.– tripleeeOct 15, 2017 at 11:30 -
2Sad to disappoint you, but on Windows and all *nix systems the main running unit is a thread. You can't run process without threads. If you exit last thread, process terminates. Oct 16, 2017 at 17:25
-
2This does not answer the question. In particular, this question is about Python. Python has a Global Interpreter Lock (GIL). If a thread were to go to sleep while holding the GIL, it would block all Python threads in the process, because they all share the same lock. Aug 19, 2019 at 20:24
it blocks a thread if it is executed in the same thread not if it is executed from the main code