简体   繁体   English

在Python中使用multiprocessing.Pipe()发送和接收异步

[英]Sending and receiving async over multiprocessing.Pipe() in Python

I'm having some issues getting the Pipe.send to work in this code. 我有一些问题让Pipe.send在这段代码中工作。 What I would ultimately like to do is send and receive messages to and from the foreign process while its running in a fork. 我最不想做的是在fork中运行时向外部进程发送和接收消息。 This is eventually going to be integrated into a pexpect loop for talking to interpreter processes. 这最终将被集成到一个pexpect循环中,用于与解释器进程交谈。

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

This works, but might need some more work. 这可行,但可能需要更多的工作。 Not sure how many of these i can create and loop 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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