user1783732
user1783732

Reputation: 1843

time.sleep seems to block other threads

I have this simple Python 3 program where a child thread sleeps and the main thread seems to be blocked as well. Why doesn't sleep switch the cpu to the main thread?

import threading
import time


def hello():
    print('hello')
    while True:
        time.sleep(10000)


threading.Thread(hello()).start()

print('world')

Output:

hello

The word world never printed.

Upvotes: 2

Views: 493

Answers (1)

Reut Sharabani
Reut Sharabani

Reputation: 31339

You are invoking hello on the main thread by using hello() this is a function call (because of the ()), not a function reference. The reference (name) is just hello.

Maybe try:

threading.Thread(target=hello).start()

Upvotes: 4

Related Questions