我具有以下片的內部運行的代碼thread..where「擴大」 C可執行產生用於每個輸入「URL」唯一字符串輸出:Python的子流程-POPEN死鎖在多線程環境中
p = Popen(["expand", url], bufsize=65536, stdout=PIPE, stderr=PIPE, close_fds=True)
output,error = p.communicate()
print output
我已經實現了一個基於Queue的多線程解決方案,它可以處理一批100個URL中的5000個URL。
當我運行腳本時;它掛..和ps -aef
顯示,2個進程仍在運行:
1. 10177 5721 6662 6 09:25 pts/15 00:04:36 python expandPlaylist.py -s -t
2. 10177 11004 5721 0 09:26 pts/15 00:00:00 expand http://www.sample.com
主python腳本堆棧跟蹤:螺紋
# ThreadID: 140332211570432
File: "expandPlaylist.py", line 902, in <module>
Main()
File: "expandPlaylist.py", line 894, in Main
startmain(db, c, conf)
File: "expandPlaylist.py", line 834, in startmain
stream_queue.join()
File: "/usr/lib64/python2.7/Queue.py", line 82, in join
self.all_tasks_done.wait()
File: "/usr/lib64/python2.7/threading.py", line 238, in wait
waiter.acquire()
堆棧跟蹤它得到了僵持
# ThreadID: 140332016596736
File: "/usr/lib64/python2.7/threading.py", line 503, in __bootstrap
self.__bootstrap_inner()
File: "/usr/lib64/python2.7/threading.py", line 530, in __bootstrap_inner
self.run()
File: "expandPlaylist.py", line 120, in run
self.process.wait()
File: "/usr/lib64/python2.7/subprocess.py", line 1242, in wait
pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
File: "/usr/lib64/python2.7/subprocess.py", line 471, in _eintr_retry_call
return func(*args)
GDB process_id的詳細資料:11004
(gdb) bt
#0 __lll_lock_wait() at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fc36bd33294 in _L_lock_999() from /lib64/libpthread.so.0
#2 0x00007fc36bd330aa in __pthread_mutex_lock (mutex=0x6a8c20) at pthread_mutex_lock.c:61
#3 0x00007fc36c204dcd in g_mutex_lock (mutex=0x6a8c50) at gthread-posix.c:213
#4 0x00007fc36c1b11df in g_source_unref_internal (source=0x844f90, context=0x6a8c50, have_lock=0) at gmain.c:1975
#5 0x00007fc36c1b13e3 in g_source_unref (source=0x844f90) at gmain.c:2044
#6 0x00007fc36cb475a9 in soup_session_dispose (object=0x61e100) at soup-session.c:305
#7 0x00007fc36c4d270e in g_object_unref (_object=0x61e100) at gobject.c:3160
#8 0x000000000040584b in dispose_session (parser=0x618020) at al_playlist_parser.c:859
#9 0x0000000000403b0b in al_playlist_parser_dispose (obj=0x618020) at al_playlist_parser.c:129
#10 0x00007fc36c4d270e in g_object_unref (_object=0x618020) at gobject.c:3160
#11 0x0000000000403315 in main (argc=1, argv=0x7fff462cdca8) at al_expand.c:143
- 我該如何避免死鎖?
- 否則是否有任何方法來將超時綁定到
self.process.wait()
並終止該線程,如果子進程花費的時間過長?
如果您希望我們幫助您查找錯誤,您必須發佈您的「基於隊列的多線程解決方案」。 – Max
1.用['subprocess32'](https://pypi.python.org/pypi/subprocess32)試試它是否是由'subprocess'模塊中已經修復的錯誤引起的。 2. [停止讀取沒有掛起的Python輸出過程?](http://stackoverflow.com/q/4417962/4279) – jfs
你能發佈一個最小的完整的代碼示例來顯示問題嗎?更好的,如果它是基於[我的回答你的上一個問題]代碼(http://stackoverflow.com/a/20064095/4279) – jfs