main_task.py
from process_template import ProcessTemplate
if __name__ == '__main__':
data = {'input': [2,0,1,9], 'result':[]}
child_1 = ProcessTemplate(data)
child_1.start()
while (1):
msg = raw_input()
if msg == "Q":
child_1.get_parent_send_conn().send("Stop now!")
if child_1.get_parent_recv_conn().poll():
print "Parent gets", child_1.get_parent_recv_conn().recv()
process_template.py
from multiprocessing import Process, Pipe
import time
class ProcessTemplate(Process):
def __init__(self, data, *args, **kwargs):
self.parent_recv_conn, self.child_send_conn = Pipe()
self.parent_send_conn, self.child_recv_conn = Pipe()
self.data = data
Process.__init__(self)
def get_parent_send_conn(self):
return self.parent_send_conn
def get_parent_recv_conn(self):
return self.parent_recv_conn
def get_child_send_conn(self):
return self.child_send_conn
def get_child_recv_conn(self):
return self.child_recv_conn
def run(self):
print self.data
time.sleep(2)
print "step", 1
self.child_send_conn.send("step 1 done")
if self.child_recv_conn.poll():
print "Child gets", self.child_recv_conn.recv()
time.sleep(2)
print "step", 2
self.child_send_conn.send("step 2 done")
if self.child_recv_conn.poll():
print "Child gets", self.child_recv_conn.recv()
time.sleep(2)
print "step", 3
self.child_send_conn.send("step 3 done")
if self.child_recv_conn.poll():
print "Child gets", self.child_recv_conn.recv()
time.sleep(2)
print "step", 4
self.child_send_conn.send("step 4 done")
if self.child_recv_conn.poll():
print "Child gets", self.child_recv_conn.recv()
С помощью приведенного выше кода основная задача не может получитьсообщение от дочернего процесса.
Помощь оценена!