How to transfer the data after the completion of the second process to the first one?
Question:
I have application X.exe and Y.exe. First, I start server X, after which I have to start Y 20 times and get 20 different PIDs. Due to certain circumstances, I have to run these processes at the same time, so I run X and the script issuing PIDs as processes. I don’t understand how to transfer the data generated by the second process to the first one in order to work with them already in it. Here is my code:
from subprocess import Popen, PIPE
from multiprocessing import Process
def firstproc():
process = Popen(
'X.exe',
stdout=PIPE)
while True:
chunk = process.stdout.readline()
print(chunk)
def secondproc():
def getPID():
return Popen('Y.exe').pid
return [getPID() for _ in range(20)]
def main():
p1 = Process(target=firstproc, args=())
p2 = Process(target=secondproc, args=())
p1.start()
p2.start()
if __name__ == "__main__":
main()
The first process runs indefinitely
Answers:
To write a process1 stdout line in any process2 stdin you can use this code:
from subprocess import Popen, PIPE
from threading import Thread, Lock
class Processes:
def __init__(self):
self.p1: Popen = None
self.p2: Popen = None
self.lock = Lock()
self.lock2 = Lock()
self.lock.acquire()
self.lock2.acquire()
self.p2_run = True
def firstproc(self):
self.p1 = Popen(
'X.exe',
stdout=PIPE)
while self.p2_run:
line = self.p1.stdout.readline()
self.lock2.acquire()
if self.p2_run:
self.p2.communicate(line)
self.lock.release()
def secondproc(self):
def getPID():
self.p2 = Popen('Y.exe', stdin=PIPE)
self.lock2.release()
self.lock.acquire()
return self.p2.pid
pids = []
for _ in range(8):
pids.append(getPID())
self.p2_run = False
self.lock2.release()
return pids
def main():
t = Processes()
p1 = Thread(target=t.firstproc, args=())
p1.start()
p2 = Thread(target=t.secondproc, args=())
p2.start()
p1.join()
if __name__ == "__main__":
main()
I use threading to synchronize processes to write in stdin and to read stdout.
I have application X.exe and Y.exe. First, I start server X, after which I have to start Y 20 times and get 20 different PIDs. Due to certain circumstances, I have to run these processes at the same time, so I run X and the script issuing PIDs as processes. I don’t understand how to transfer the data generated by the second process to the first one in order to work with them already in it. Here is my code:
from subprocess import Popen, PIPE
from multiprocessing import Process
def firstproc():
process = Popen(
'X.exe',
stdout=PIPE)
while True:
chunk = process.stdout.readline()
print(chunk)
def secondproc():
def getPID():
return Popen('Y.exe').pid
return [getPID() for _ in range(20)]
def main():
p1 = Process(target=firstproc, args=())
p2 = Process(target=secondproc, args=())
p1.start()
p2.start()
if __name__ == "__main__":
main()
The first process runs indefinitely
To write a process1 stdout line in any process2 stdin you can use this code:
from subprocess import Popen, PIPE
from threading import Thread, Lock
class Processes:
def __init__(self):
self.p1: Popen = None
self.p2: Popen = None
self.lock = Lock()
self.lock2 = Lock()
self.lock.acquire()
self.lock2.acquire()
self.p2_run = True
def firstproc(self):
self.p1 = Popen(
'X.exe',
stdout=PIPE)
while self.p2_run:
line = self.p1.stdout.readline()
self.lock2.acquire()
if self.p2_run:
self.p2.communicate(line)
self.lock.release()
def secondproc(self):
def getPID():
self.p2 = Popen('Y.exe', stdin=PIPE)
self.lock2.release()
self.lock.acquire()
return self.p2.pid
pids = []
for _ in range(8):
pids.append(getPID())
self.p2_run = False
self.lock2.release()
return pids
def main():
t = Processes()
p1 = Thread(target=t.firstproc, args=())
p1.start()
p2 = Thread(target=t.secondproc, args=())
p2.start()
p1.join()
if __name__ == "__main__":
main()
I use threading to synchronize processes to write in stdin and to read stdout.