2017-12-05 3 views
0

나는 멀티 스레드 동적 데모를 달성하고 싶다. 프로그램이 실행되기 시작하면, 플롯을 보여주고 모든 것이 OK이다. 그러나 명백하게 비정상적으로 실행되면 마우스 포인터가 회전하는 원이된다. TclStackFree : 잘못된 freePtr입니다. 순서가 맞지 않아요? 파이썬 프로그램에서

#-*-coding:utf-8-*- 
import matplotlib 
from matplotlib.patches import Circle 
import dask 

import matplotlib.pyplot as plt 
import xlrd 
import numpy as np 
from matplotlib.animation import FuncAnimation 
import matplotlib.ticker as mticker 
import cartopy.crs as ccrs 
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER 
import time 
from matplotlib.offsetbox import AnnotationBbox,OffsetImage 
from PIL import Image 
import random 
from time import ctime,sleep 
import threading 

#matplotlib.use('Agg') 
#地图可视化 
fig=plt.figure(figsize=(20,10)) 
ax = plt.axes(projection=ccrs.PlateCarree()) 
ax.coastlines() 
ax.stock_img() 
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, 
        linewidth=2, color='gray', alpha=15, linestyle='--') 
gl.xlabels_top = False 
gl.ylabels_left = False 
gl.xlines = False 
gl.xlocator = mticker.FixedLocator([-180, -45, 0, 45, 180]) 
gl.xformatter = LONGITUDE_FORMATTER 
gl.yformatter = LATITUDE_FORMATTER 
gl.xlabel_style = {'size': 15, 'color': 'gray'} 
gl.xlabel_style = {'color': 'red', 'weight': 'bold'} 
img=Image.open(r'E:\python_file\untitled\p.png') 
imagebox=OffsetImage(img,zoom=0.05) 
imagebox.image.axes=ax 
ab=AnnotationBbox(imagebox,[55,10],pad=0,frameon=False) 
ax.add_artist(ab) 
ac=AnnotationBbox(imagebox,[63,0],pad=0,frameon=False) 
ax.add_artist(ac) 
ad=AnnotationBbox(imagebox,[70,-10],pad=0,frameon=False) 
ax.add_artist(ad) 
#============================================#攻击 
tolerance=1 
x_m1,y_m1=random.randint(-180,180),random.randint(-90,90) 
v_m1=170 
x_m2,y_m2=random.randint(-180,180),random.randint(-90,90) 
v_m2=v_m1 
x_m3,y_m3=random.randint(-180,180),random.randint(-90,90) 
v_m3=v_m1 
x_m4,y_m4=55,10 
x_m5,y_m5=63,0 
x_m6,y_m6=70,-10 

class target(): 
    """docstring for target""" 
    def __init__(self, x, y): 

     self.x = x 
     self.y = y 
target1=target(x_m4,y_m4) 
target2=target(x_m5,y_m5) 
target3=target(x_m6,y_m6) 
v=v_m1 


class missile(threading.Thread): 

    """docstring for missile""" 

    def __init__(self, x, y,name): 
     super(missile,self).__init__() 
     self.x = x 
     self.y = y 
     self.name=name 
    def forward(self, v, target1): 
     """docstring for forward""" 
     if self.x < target1.x: 
      alpha = np.arctan((target1.y - self.y)/(target1.x - self.x)) 
     elif self.x > target1.x: 
      alpha = np.pi + np.arctan((target1.y - self.y)/(target1.x - self.x)) 
     elif self.x == target1.x and self.y < target1.y: 
      alpha = np.pi/2 
     else: 
      alpha = -np.pi/2 
     self.x = self.x + v * 0.01 * np.cos(alpha) 
     self.y = self.y + v * 0.01 * np.sin(alpha) 
     return self.x, self.y 
    def distance(self, target1): 
     """docstring for distance""" 
     return np.sqrt((self.x - target1.x) ** 2 + (self.y - target1.y) ** 2) 
    def run(self): 
     while True: 
      if self.distance(target1) < tolerance or self.distance(target2) < tolerance or self.distance(
        target3) < tolerance: 
       print ("collision") 
       break 
      if self.distance(target1) < self.distance(target2) and self.distance(target1) < self.distance(target3): 
       self.x, self.y = self.forward(v, target1) 

      if self.distance(target2) < self.distance(target1) and self.distance(target2) < self.distance(target3): 
       self.x, self.y = self.forward(v, target2) 

      if self.distance(target3) < self.distance(target2) and self.distance(target3) < self.distance(target1): 
       self.x, self.y = self.forward(v, target3) 
      plt.plot(self.x, self.y, 'o') 

      fig.canvas.draw() 

      fig.canvas.flush_events() 




m2=missile(x_m2,y_m2,'mm') 
m1=missile(x_m1,y_m1,'mn') 
m3=missile(x_m3,y_m3,'md') 
print "m1前" 
m1.start() 
print "m1后" 
m2.start() 
print "m2后" 
m3.start() 
print "m3后" 

plt.show() 

이 프로그램이 정상적으로 실행하기 위해 충돌을 해결하기 위해 몇 가지 제안이있을 수 다음과 같이 그런 다음 tltle.The 전체 코드에서 오류 코드와 프로그램 crashs 및 종료는? 제 생각에는, 문제가 마침내

+0

를 해결합니다. – ImportanceOfBeingErnest

답변

0

멀티 스레딩 관련이있다, 내가 자원 회계 자료는 플롯 과정에 대한 잠금을 multithreadings.putting에있다 찾으 [mcve] 제발 봐 문제를