python - Matplotlib 和 :RuntimeError: main thread is not in main loop:

标签 python multithreading matplotlib tkinter multiprocessing

我正在尝试在 python 下并行运行多个重复任务。我对多重处理还很陌生,但由于所有任务都是独立的,我使用了以下简单的代码:

    import numpy as np
    import sys
    import os
    import glob
    import matplotlib.pyplot as plt
    import concurrent.futures as Cfut
    
    def analize(simul, N_thread):
        path = os.getcwd()

        print('Analizing topo ...')
        Data = output of some calculations
    
        print('Writing Data ...')
        np.save('Data', Data)
    
        print('Data saved')
        print('Printing figures')

        plt.figure()
        plt.plot(Data[0])
        plt.savefig('figure1.pdf')

        plt.clf()
        plt.plot(Data[0])
        plt.savefig('figure1.pdf')

        plt.close('all')
        print('figures saved')
        os.chdir(path

if __name__ == '__main__':
    list_simul = sorted(glob.glob('Layer*'))
    executor = Cfut.ProcessPoolExecutor(5)
   #executor = Cfut.ThreadPoolExecutor(N_jobs)
    futures = [executor.submit(analize, item, 10) for item in list_simul]
    Cfut.wait(futures)

它在 3 个月内运行得很好,从早上开始我有时会遇到以下错误:

Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x7fac6c187f98>>
    Traceback (most recent call last):
      File "/usr/lib/python3.5/tkinter/__init__.py", line 3359, in __del__
        self.tk.call('image', 'delete', self.name)
    RuntimeError: main thread is not in main loop

奇怪的是,有些工作完成时没有任何问题,而且这种情况并不总是发生。经过一番研究,我发现了很多关于这个问题的帖子,它们都与 GUI 有关。我理解这个问题,但我想我应该能够找到解决方法,因为我没有显示任何图形,只是创建对象并保存它,并且所有任务都是独立的。

有什么提示吗?

最佳答案

如图所示here ,您是否尝试过将以下代码添加到导入的顶部?

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

关于python - Matplotlib 和 :RuntimeError: main thread is not in main loop:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52839758/

相关文章:

python - 如何在python中找到中间的数字

Python: 'super' 对象没有属性 'attribute_name'

linux - 如何查看哪个 CPU 上运行了哪些进程?

python - 用线程在tkinter中的进度条

c# - 停止程序执行直到线程完成

python - 如何在 Python 中将 x 刻度调整为字符串值

python - 将交互式 WebAgg 绘图嵌入 Tornado 时出现问题

python - 频率值的 R 分位数

Python 数量限制

python - 如何将 x 轴从普通数字更改为日期和时间格式?