2010-05-14 24 views
3

이 코드에서 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() 
    ` 

답변

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) 
+0

1이 질문에 감사드립니다. .poll()이 (가) 끊어졌습니다. – John