发送和Python中接收异步过multiprocessing.Pipe() [英] Sending and receiving async over multiprocessing.Pipe() in Python
本文介绍了发送和Python中接收异步过multiprocessing.Pipe()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些问题得到Pipe.send在这个code工作。我最终会做的是发送和接收消息,并从国外进程,而其在运行叉。这最终将要被集成到一个Pexpect的循环为交谈间preTER流程。
从多处理导入过程中,管
从进口Pexpect的产卵
类CockProc(进程): 高清启动(个体经营):
self.process =产卵('coqtop',['-emacs-U']) DEF运行(个体经营,康涅狄格州):
而真正的:
如果不是conn.poll():
CMD = conn.recv()
self.process.send(CMD)
self.process.expect('\\< \\ /提示\\>')
结果= self.process.before + self.process.after +
conn.send(结果)
Q,P =管道()PROC = CockProc()
proc.start()
proc.run(p)的
RES = q.recv()
命令的raw_input =(RES +)q.send(命令)
RES = q.recv()
parent_conn.send('OHHAI')
p.join()
`
解决方案
这工作,但可能需要一些更多的工作。不知道我怎么这么多可以通过创建和循环。
从多处理导入过程中,管
从进口Pexpect的产卵
类CockProc(进程): 高清启动(个体经营):
self.process =产卵('coqtop',['-emacs-U']) DEF运行(个体经营,康涅狄格州):
如果conn.poll():
CMD = conn.recv()
self.process.send(CMD +\\ n)
打印发送COMM
self.process.expect('\\< \\ /提示\\>')
结果= self.process.before + self.process.after +
conn.send(结果)在这里,有=管道(双面= TRUE)PROC = CockProc()
proc.start()
proc.run(那里)而真正的:
如果here.poll():
RES = here.recv()
命令的raw_input =(RES +)
here.send(命令)
proc.run(那里)
I'm having some issues getting the Pipe.send to work in this code. What I would ultimately like to do is send and receive messages to and from the foreign process while its running in a fork. This is eventually going to be integrated into a pexpect loop for talking to 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()
`
解决方案
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)
这篇关于发送和Python中接收异步过multiprocessing.Pipe()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文