multithreading - Python thread holds up the rest of the script -
running python 3.2 on windows 7 pro 64 bit.
ok have basic code here that's not behaving want 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 script runs, starts thread sleeps 5 seconds prints out 'i have executed'. in meantime script keeps going , prints out 'i go first'.
what happens script starts thread, waits finish , continues. i'm not doing threading correctly i'm having trouble finding simple examples of threading python 3.
your statement:
threading.thread(target = shutdown(5)).start()
can equivalently written as:
x = shutdown(5) threading.thread(target = x).start()
i.e. calling shutdown first, passing result thread constructor.
you need pass function, without calling it, , argument list, thread separately:
threading.thread(target = shutdown, args = (5,)).start()
Comments
Post a Comment