gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:31:14 37 4
gpt4 key购买 nike

我正在尝试在 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/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com