Below is my test code.
When running with python2.7 it shows that the program won't receive any signal until all spawned threads finish.
While with python3.2, only the main thread's sigintHandler gets called.
I'am confused with how python handles threads and signal, so how do I spawn a thread and do signal handling within that thread? Is it possible at all?
from __future__ import print_function
from threading import Thread
import signal, os, sys
from time import sleep
def sigintHandler(signo, _):
print("signal %d caught"%signo)
def fn():
print("thread sleeping")
sleep(10)
print("thread awakes")
signal.signal(signal.SIGINT, sigintHandler)
ls = []
for i in range(5):
t = Thread(target=fn)
ls.append(t)
t.start()
print("All threads up, pid=%d"%os.getpid())
for i in ls:
i.join()
while True:
sleep(20)