简体   繁体   中英

How to run two python scripts in parallel on Windows

I currently have two python scripts, one is for writing data into a file and the other one is for reading data continuously from that file at the same time and do plotting. I want to create a third script which can automatically run these two scripts at the same time ( actually one slightly ahead of another because the file needs to be created for the other script to read from it).

So here is my code:

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

for x in range (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()

And the plotting script:

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()

Any advice would be appreciated!

My current solution is:

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]

By running the above code, the graph won't be able to show until the writing function is finished.

use the multiprocessing class. you can start a new process to write to the file, then call p.join() to make the main function wait for the first process to finish, then start the second process. see here: http://docs.python.org/2/library/multiprocessing.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM