小编Bor*_*ris的帖子

Matplotlib:在多个线程中同时绘图

我正在尝试并行绘图以更快地完成大批量作业.为此,我为我计划制作的每个剧情开始一个主题.

我曾希望每个线程都能完成它的绘图并自行关闭(据我所知,当Python通过run()中的所有语句时,它会关闭线程).下面是一些显示此行为的代码.

如果创建图形的行已注释掉,则按预期运行.另一个看似合情合理的消息是,当你只生成一个线程时,它也会按预期运行.

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) …
Run Code Online (Sandbox Code Playgroud)

python multithreading matplotlib python-multithreading

19
推荐指数
2
解决办法
2万
查看次数