大きなバッチジョブをより速く終了するために、いくつかのプロットを並行して実行しようとしています。この目的のために、私は作成を計画している各プロットのスレッドを開始します。Matplotlib:複数のスレッドで同時にプロットする
私は、各スレッドがプロットを終了して終了することを望んでいました(私が理解するように、Pythonはrun()内のすべてのステートメントを通過するときにスレッドを閉じます)。以下は、この動作を示すコードです。
図を作成する行がコメントアウトされている場合、期待どおりに実行されます。もう一つの役に立つ点は、あなたが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()
助けを歓迎します。
をあなたが実際にうまくいかない何を言っていませんでした、それはのように聞こえるが、何らかのスレッドの並行性の問題。 –
私は実際に何がうまくいかないのか分かりません。私はエラーを出さず、Pythonのプロセスは実行し続けます。また、メインスレッドのprint文は1秒後に実行されますが、1秒後には実行されません。タスクマネージャーの一見は、プロセスが多くのCPUを使い続けていることを示しています。私は残念ながら、深刻なデバッグの経験が限られています。 – Boris
'makeTapHist()。start()'を複数回呼び出すつもりですか?おそらくループの外側にあるように見えます。 –