【问题标题】:Sending and receiving async over multiprocessing.Pipe() in Python在 Python 中通过 multiprocessing.Pipe() 发送和接收异步
【发布时间】:2010-05-14 01:21:06
【问题描述】:

我在让 Pipe.send 在此代码中工作时遇到了一些问题。我最终想做的是在外部进程在分叉中运行时向外部进程发送和接收消息。这最终将被集成到与解释器进程对话的 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()
    `

【问题讨论】:

    标签: python multiprocessing asynchronous


    【解决方案1】:

    这可行,但可能需要更多工作。不确定我可以创建和循环其中的多少。

    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 谢谢我有这个问题,我正在挂断 .poll()
    猜你喜欢
    • 1970-01-01
    • 2013-07-19
    • 2018-05-03
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多