418

In Python for *nix, does time.sleep() block the thread or the process?

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Jeremy Dunck
  • 5,724
  • 6
  • 25
  • 30

7 Answers7

417

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
Nick Bastin
  • 30,415
  • 7
  • 59
  • 78
  • 4
    How 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. – akki Aug 01 '17 at 01: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). – Nick Bastin Aug 01 '17 at 02:32
  • 19
    Open 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. – akki Aug 01 '17 at 05: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. – Jeff Younker Oct 20 '17 at 14:37
  • Second answer: His point is that while 'waiter()' is sleeping, 'worker' continues to run. – Jeff Younker Oct 20 '17 at 14:39
  • 3
    akki, 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) – gunit Dec 01 '17 at 23:32
63

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!

Neuron
  • 5,141
  • 5
  • 38
  • 59
Zach Burlingame
  • 13,476
  • 14
  • 56
  • 65
  • 5
    @MichaelMrozek: [`sleep(3)` says: *"sleep() makes the calling thread sleep until seconds seconds have elapsed or a signal arrives which is not ignored."*](http://man7.org/linux/man-pages/man3/sleep.3.html) And [there is Python documentation bug](http://bugs.python.org/issue23251). – jfs Jan 17 '15 at 04:02
40

Just the thread.

finnw
  • 47,861
  • 24
  • 143
  • 221
19

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.

Smi
  • 13,850
  • 9
  • 56
  • 64
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
4

Only the thread unless your process has a single thread.

Ali Abbasinasab
  • 382
  • 3
  • 13
3

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.

Denis The Menace
  • 505
  • 4
  • 10
  • 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. – tripleee Oct 15 '17 at 11:30
  • 2
    Sad 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. – Denis The Menace Oct 16 '17 at 17:25
  • 2
    This 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. – Cort Ammon Aug 19 '19 at 20:24
1

it blocks a thread if it is executed in the same thread not if it is executed from the main code