0
成功嘗試之後掛在口Python解釋器掛起 - 與線程和隊列
import threading
import Queue as queue
import time
import sys
class WorkItem(threading.Thread):
def __init__(self):
self.P1 = 20
self.P2 = 40
threading.Thread.__init__(self)
def run(self):
print "P1 = %d" % (self.P1)
print "P2 = %d" % (self.P2)
class WorkQueue(object):
def __init__(self,queueLimit = 5):
self.WorkQueue = queue.Queue(queueLimit)
self.dispatcherThread = threading.Thread(target=self.DequeueWorker)
self.dispatcherThread.start()
self.QueueStopEvent = threading.Event()
self.QueueStopEvent.clear()
def DequeueWorker(self):
print "DequeueWorker Enter .."
while not self.QueueStopEvent.isSet():
workItem = self.WorkQueue.get(True)
workItem.start()
def DispatchToQueue(self,workItem):
self.WorkQueue.put(workItem,True)
def Stop(self):
self.QueueStopEvent.set()
self.queue = None
def main():
q = WorkQueue()
for i in range(1,20):
t = WorkItem()
q.DispatchToQueue(t)
time.sleep(10)
q.Stop()
if __name__ == "__main__":
main()
我可以看到DequeueWorker是一個仍在運行和正在申請,並試圖瞭解爲什麼,因爲我做信號Stop事件。我期待循環會退出。
>>> $w
=> Frame id=0, function=DequeueWorker
Frame id=1, function=run
Frame id=2, function=__bootstrap_inner
Frame id=3, function=__bootstrap
幫助讚賞!
不應該在這種情況下獲得堆棧調用的頂部,因爲我只能看到幀ID = 0,函數= DequeueWorker作爲調試時的第一幀 – Addy
@Addy我不能說你特定的調試解決方案正在使用,但我知道如果我在'get'調用之前和之後插入'print'語句,清楚地說明在隊列耗盡後'get'永遠不會返回(並且第二個'print'語句不會執行)。 – rchang
這是一個Visual Studio python工具,它也是相同的pdb – Addy