小编Lui*_*des的帖子

matplotlib在简单的线程中绘制冻结

一直在玩Python的绘图库,并且遇到了matplotlib,它似乎已经过战斗测试和验证.但是我在线程中创建一个简单的绘图时遇到了一个问题.

在下面的示例中,Dummyplotme方法连续两次在一个线程中运行,但在第二次迭代中它被卡住/冻结.很可能是明显的并且与线程本身有关,但到目前为止我没有发现它.

import matplotlib.pyplot as plt
from numpy import arange, sin, pi
import threading

class Dummy():

    def plotme(self, iteration = 1):

        print "%ix plotting... " % iteration,
        t = arange(0.0, 2.0, 0.01)
        s = sin(2*pi*t)
        plt.plot(t, s)
        plt.xlabel('time (s)')
        plt.ylabel('voltage (mV)')
        plt.title('About as simple as it gets, folks')
        #savefig("test.png") # irrelevant here
        plt.close()

    def threadme(self, iteration = 1):

        thread_plot = threading.Thread(target=self.plotme,
                                      args=(iteration,))
        thread_plot.start()
        thread_plot.join()

dummy = Dummy()
dummy.threadme(1)
dummy.threadme(2)
Run Code Online (Sandbox Code Playgroud)

python plot multithreading matplotlib freeze

4
推荐指数
1
解决办法
3976
查看次数

标签 统计

freeze ×1

matplotlib ×1

multithreading ×1

plot ×1

python ×1