Running Python 3.2 on Windows 7 Pro 64 Bit.
OK I have some very basic code here that’s just not behaving like I want it to.
#!/usr/bin/env python
import time
import threading
def shutdown(sleeptime):
time.sleep(sleeptime)
print('I have executed')
threading.Thread(target = shutdown(5)).start()
print('I go first')
The idea being that the script runs, it starts a thread which sleeps for 5 seconds then prints out ‘I have executed’. In the meantime the script keeps going and prints out ‘I go first’.
What actually happens is that the script starts the thread, everything waits for it to finish and then it continues. Clearly I’m not doing the threading correctly but I’m having trouble finding simple examples of threading with Python 3.
Your statement:
can be equivalently written as:
I.e. you are calling shutdown first, then passing the result to the Thread constructor.
You need to pass your function, without calling it, and your argument list, to Thread separately: