大規模なバッチジョブをより迅速に完了するために、いくつかのプロットを並行して実行しようとしています。この目的のために、作成する予定のプロットごとにスレッドを開始します。
各スレッドがプロットを終了し、それ自体を閉じることを期待していました (私が理解しているように、Python は run() 内のすべてのステートメントを通過するとスレッドを閉じます)。以下は、この動作を示すコードです。
Figure を作成する行がコメント アウトされている場合、期待どおりに実行されます。もう 1 つの有益なヒントは、スレッドを 1 つだけ生成しても期待どおりに動作することです。
import matplotlib.pyplot as plt
import time
import Queue
import threading
def TapHistplots():
## for item in ['str1']:
# # it behaves as expected if the line above is used instead of the one below
for item in ['str1','str2']:
otheritem = 1
TapHistQueue.put((item, otheritem))
makeTapHist().start()
class makeTapHist(threading.Thread):
def run(self):
item, otheritem = TapHistQueue.get()
fig = FigureQueue.get()
FigureQueue.put(fig+1)
print item+':'+str(fig)+'\n',
time.sleep(1.3)
plt.figure(fig) # comment out this line and it behaves as expected
plt.close(fig)
TapHistQueue = Queue.Queue(0)
FigureQueue = Queue.Queue(0)
def main():
start = time.time()
"""Code in here runs only when this module is run directly"""
FigureQueue.put(1)
TapHistplots()
while threading.activeCount()>1:
time.sleep(1)
print 'waiting on %d threads\n' % (threading.activeCount()-1),
print '%ds elapsed' % (time.time()-start)
if __name__ == '__main__':
main()
どんな助けでも正当に評価されます。