找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 3936|回复: 2

[求助] 多线程绘图出错(matplotlib、threading)

1

主题

3

帖子

3

积分

贫民

积分
3
longfengpili 发表于 2018-1-21 11:28:45 | 显示全部楼层 |阅读模式
多线程绘图的时候发生错误,跪求大神指点!
-------------------------------以下是代码--------------------------------------
from threading import Thread,Lock
import time
import datetime
import matplotlib.pyplot as plt
import random

lock = Lock()

def run(i):
    for j in range(2):
        print('{}-{}开始'.format(i,j))
        a = datetime.datetime.now()
        lock.acquire()
        time.sleep(1)
        draw_day(i,j)
        lock.release()
        b = datetime.datetime.now()
        end = '{}-{}结束,{},本次共运行了{}秒!'.format(i,j,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),(b - a).seconds)
        print(end)


def draw_day(i,j):
    x1 = [1,2,9]
    y1 = [1,8,20]
    x2 = [2,3,12]
    y2 = [8,40,18]

    plt.figure(figsize=(10,6))
    plt.plot(x1,y1)
    plt.plot(x2,y2)
    #plt.show()
    plt.savefig('{}-{}.png'.format(i,j))
    plt.close()


def Thread_w():
    Threads = []
    for k in range(3):
        t = Thread(target=run,args=(k,),name=k)
        Threads.append(t)

    for t in Threads:
        t.start()

    for t in Threads:
        t.join()

print('='*50)
while True:
    a = datetime.datetime.now()
    Thread_w()
    b = datetime.datetime.now()
    end = '{},本次共运行了{}秒!'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),(b - a).seconds)
    print(end)
    print('='*50)


----------------------以下是报错内容-----------------------------
==================================================
0-0开始
1-0开始
2-0开始
0-0结束,2018-01-21 11:27:11,本次共运行了1秒!
0-1开始
1-0结束,2018-01-21 11:27:12,本次共运行了2秒!
1-1开始
2-0结束,2018-01-21 11:27:13,本次共运行了3秒!
2-1开始
0-1结束,2018-01-21 11:27:14,本次共运行了3秒!
1-1结束,2018-01-21 11:27:16,本次共运行了3秒!
2-1结束,2018-01-21 11:27:17,本次共运行了3秒!
2018-01-21 11:27:17,本次共运行了7秒!
==================================================
0-0开始
1-0开始
2-0开始
0-0结束,2018-01-21 11:27:19,本次共运行了1秒!
0-1开始
1-0结束,2018-01-21 11:27:20,本次共运行了2秒!
1-1开始
2-0结束,2018-01-21 11:27:21,本次共运行了3秒!
2-1开始
0-1结束,2018-01-21 11:27:22,本次共运行了3秒!
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8ABF86518>>
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8ABF86B00>>
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8BAE23668>>
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8BABAE278>>
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8AEBAAFD0>>
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8AEE57400>>
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8AF12CF60>>
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8AF842320>>
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8B297F3C8>>
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread





回复

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
longfengpili  楼主| 发表于 2018-1-22 12:54:21 | 显示全部楼层
求救啊。。。。。。。。。
回复

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
longfengpili  楼主| 发表于 2018-1-30 13:29:01 | 显示全部楼层
人工置顶
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表