I’m hoping that someone has a quick and easy solution to this issue that I’m facing. I’m working through chapter 5 of the 4th edition of the Programming Python book by Mark Lutz and I’m having some troubles beginning on page 189. Basically, there is a very simple example:
import _thread
def action(i):
print(i ** 32)
_thread.start_new_thread(action, (2, ))
For some reason, this script will not produce any output on my pc running Ubuntu 12.04, but will on my windows 7 machine. The output when run from the terminal is:
un@homepc:~/Desktop/pp$ python3.2 thread1.py
un@homepc:~/Desktop/pp$
Any help would be much appreciated.
Thanks.
I am not an expert of threads but this is most likely the problem:
When running this code, Python has nothing to do after the last line;so it exits forcefully ending the thread (whether it completed or not).
Depending on how the threads run, the thread may complete or it may not. (unreliable behaviour)
main thread pseudo-code instruction:
create the new thread
new thread:
action(2)
main thread next instruction:
program ended;exit
Fixed code: