我有一个简单的python脚本,该脚本可连接到Arduino设备并通过串行连接创建一些图形。这些图必须实时更新,以便我产生一些流程来做到这一点,这样我的主体就不会阻塞。
源代码按预期运行(没有错误,没有崩溃等),但是当我在测试虚拟机上使用pyinstaller对其进行编译时,我创建的代码似乎具有意外行为。进程不是从我提供的目标函数而是从我的主函数开始执行。
如何停止这种行为并获得正常的行为?
这是我的简化代码:
from multiprocessing import Process
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
def animateSpeed(i):
fig1 = plt.figure(1)
ax1 = fig1.add_subplot(1,1,1)
ax1.set_title('Speed Graph')
ax1.set_ylabel('Speed')
ax1.set_xlabel('Hits')
graph_data1 = open('./plotfiles/speedplot.txt').read()
lines1 = graph_data1.split('\n')
x1s = []
y1s = []
for line in lines1:
if len(line) > 1:
x,y = line.split(',')
y = float(y)
x = int(x)
x1s.append(x)
y1s.append(y)
ax1.clear()
ax1.set_title('Speed Graph')
ax1.set_ylabel('Speed')
ax1.set_xlabel('Hits')
ax1.plot(x1s, y1s)
def runGraph1(fig1 = None):
fig1 = plt.figure(1)
ax1 = fig1.add_subplot(1,1,1)
ax1.set_title('Speed Graph')
ax1.set_ylabel('Speed')
ax1.set_xlabel('Hits')
speedplot = animation.FuncAnimation(fig1, animateSpeed, interval=500)
plt.show()
def parse(chunk,printer_name, picture, cam_num):
speed = (chunk[offset2].split())[1]
speedfile = open("./plotfiles/speedplot.txt","a+")
speedfile.write(str(total_hits)+","+str(speed)+"\n")
if __name__ == "__main__":
p1 = Process(target=runGraph1)
p1.start()
com = select_com()
print("selected com is:"+com+"\n")
try:
ser = serial.Serial(com, 115200, timeout=1)
except Exception as e:
raise e
lineBuf = []
waiting_for_data= True
stop = False
while (stop != True):
line = ser.readline().decode('utf-8').strip('\r\n')
if len(line) == 0 or re.match(' \r\n', line):
continue
# if waiting_for_data:
lineBuf.append(line)
# if "Finished This Round" in line:
# print("Finished This Round, about to put: '", lineBuf, "'in myQueue")
# # myQueue.put(lineBuf)
# parse(lineBuf)
# break
if (re.match('[-]+', line)) and (lineBuf != []):
del lineBuf[-1] # remove "---------------'s"
# print("grammes, about to put: '", lineBuf, "'in myQueue")
waiting_for_data = not waiting_for_data
# myQueue.put(lineBuf)
parse(lineBuf,printer_name, picture,cam_num)
# if waiting_for_data:
lineBuf = []
ser.close()
我检查了multiprocessing和pyinstaller的已知问题,但我认为这不适用于我的情况。
我该如何规避/绕过?