0
我目前有兩個python腳本,一個用於將數據寫入文件,另一個用於同時從該文件中讀取數據並進行繪圖。我想創建第三個腳本,它可以同時自動運行這兩個腳本(實際上稍微超前於另一個腳本,因爲需要爲其他腳本創建文件來讀取它)。如何在Windows上並行運行兩個python腳本
因此,這裏是我的代碼:
import serial
import sys
import Queue
import threading
import scipy.io
import numpy as num
from mpl_toolkits.mplot3d import *
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
import matplotlib.animation as animation
import time
from time import sleep
AngleText = open ("data2.txt" , "w") # open file for writing data
a= 1
b= 1
c =1
在範圍X(0,20):
count = 0
sleep(0.5)
AngleText.writelines (str(a)+',')
AngleText.writelines (str(b)+',')
AngleText.writelines (str(c)+'\n')
count = count +1
a= a + 1
b= b + 2
c= c + 3
AngleText.flush()
而繪製腳本:
from mpl_toolkits.mplot3d import *
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import matplotlib
import threading as thrd
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
def animate(i):
pullData = open ('data2.txt','r').read()
dataArray = pullData.split('\n')
xar = []
yar = []
zar = []
for eachLine in dataArray:
if len(eachLine)>1:
x,y,z = eachLine.split(',')
xar.append(float(x))
yar.append(float(y))
zar.append(float(z))
tx = xar.pop(0)
Floatx= float(tx)
ty = yar.pop(0)
Floaty= float(ty)
tz = zar.pop(0)
Floatz= float(tz)
x1 = [164, 94, 0, -100.5]
x2 = [164, 94, 0, -100.5]
y1= [-72.5, -103.5, -103.5, -134.5]
y2= [72.5, 103.5, 103.5, 134.5]
z1 = [112, 60, 3, 3]
z2 = [112, 60, 3, 3]
ax.plot(x1, y1, z1, color = 'b')
plt.hold(True)
ax.plot(x2, y2, z2, color = 'r')
plt.hold(True)
ax.scatter(Floatx, Floaty, Floatz)
plt.hold(False)
ani = animation.FuncAnimation(fig,animate, interval = 25)
ax.set_xlabel('x label')
ax.set_ylabel('y label')
ax.set_zlabel('z label')
plt.show()
任何意見將是不勝感激!
我目前的解決辦法是:
import os
from subprocess import *
import time
from time import sleep
p = Popen([r'testing.py'], shell=True, stdin=PIPE, stdout=PIPE)
output = p.communicate()
print output[0]
sleep(0.5)
#run child script 2
p = Popen([r'realPlot2.py'], shell=True, stdin=PIPE, stdout=PIPE)
output = p.communicate()
print output[0]
通過運行上面的代碼,該圖將無法顯示,直到寫入功能完成。
好吧,那是否意味着我需要將這兩個代碼合併爲一個? – user2962665