I have a simple threaded Python program following the standard paradigm:
class SearchThread(threading.Thread):
def __init__(self, search_queue):
threading.Thread.__init__(self)
self.search_queue = search_queue
def run(self):
while True:
try:
search_url = self.search_queue.get(timeout=15)
# <do Internet search and print output/>
except Queue.Empty:
self.search_queue.task_done()
break
except Exception, e:
print e
if __name__ == '__main__':
search_queue = Queue.Queue()
for i in range(200):
t = SearchThread(search_queue)
t.setDaemon(True)
t.start()
search_queue.join()
The queue is filled with about 1000 urls and simple HTTP GET
is performed in <do Internet search and print output/>
. The problem is that after processing some 500-700 entries (which takes only seconds), the program consistently hangs forever with no output, no exception, nothing.
I've tried requests
, urllib2
, urllib3
, httplib2
for the HTTP GET
but nothing changes.
How do you debug hanging threaded Python program?
BTW, I'm using Python 2.7 under Ubuntu 11.10 (64bit).
I'm as clueless as before when staring at the gdb trace on the hang process --
sudo gdb python 9602
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
...
(gdb) where
#0 0x00007fc09ea91300 in sem_wait () from /lib/x86_64-linux-gnu/libpthread.so.0
#1 0x00000000004ed001 in PyThread_acquire_lock ()
#2 0x00000000004f02de in ?? ()
#3 0x00000000004b6569 in PyEval_EvalFrameEx ()
#4 0x00000000004bcd2d in PyEval_EvalCodeEx ()
#5 0x00000000004b6a5b in PyEval_EvalFrameEx ()
#6 0x00000000004b6d77 in PyEval_EvalFrameEx ()
#7 0x00000000004bcd2d in PyEval_EvalCodeEx ()
#8 0x00000000004bd802 in PyEval_EvalCode ()
#9 0x00000000004dcc22 in ?? ()
#10 0x00000000004dd7e4 in PyRun_FileExFlags ()
#11 0x00000000004de2ee in PyRun_SimpleFileExFlags ()
#12 0x00000000004ee6dd in Py_Main ()
#13 0x00007fc09d86030d in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#14 0x000000000041cb69 in _start ()
I wrote a module that prints out threads that hang longer that 10 seconds at one place. hanging_threads.py (package)
Here is an example output:
-------------------- Thread 5588 --------------------
File "C:\python33\lib\threading.py", line 844, in _exitfunc
t.join()
File "C:\python33\lib\threading.py", line 743, in join
self._block.wait()
File "C:\python33\lib\threading.py", line 184, in wait
waiter.acquire()
This occurs at the exit of the main thread when you forget to set another thread as daemon.