Sending and receiving async for multiprocessing. Pipe () in Python

I am having trouble getting Pipe.send to work in this code. What I ultimately would like to do is send and receive messages from an external process while it is running in a fork. Ultimately, this will be integrated into the pexpect loop to communicate with the interpreter processes.

from multiprocessing import Process, Pipe
from pexpect import spawn


class CockProc(Process):

    def start(self):
        self.process = spawn('coqtop', ['-emacs-U'])

    def run(self, conn):
        while True:
            if not conn.poll():
                cmd = conn.recv()
                self.process.send(cmd)
            self.process.expect('\<\/prompt\>')
            result = self.process.before + self.process.after + " "
            conn.send(result)


q, p = Pipe()

proc = CockProc()
proc.start()
proc.run(p)
res = q.recv()
command = raw_input(res + " ")

q.send(command)
res = q.recv()
parent_conn.send('OHHAI')
p.join()
    `
+3
source share
1 answer

This works, but one more job may be required. Not sure how many of them I can create and iterate over.

from multiprocessing import Process, Pipe
from pexpect import spawn


class CockProc(Process):

    def start(self):
        self.process = spawn('coqtop', ['-emacs-U'])

    def run(self, conn):
        if conn.poll():
            cmd = conn.recv()
            self.process.send(cmd + "\n")
            print "sent comm"
        self.process.expect('\<\/prompt\>')
        result = self.process.before + self.process.after + " "
        conn.send(result)

here, there = Pipe(duplex=True)

proc = CockProc()
proc.start()
proc.run(there)

while True:
    if here.poll():
        res = here.recv()
        command = raw_input(res + " ")
        here.send(command)
    proc.run(there)
+1
source

Source: https://habr.com/ru/post/1745384/


All Articles