将数据返回到调用子进程的原始进程

时间:2012-05-21 16:43:53

标签: python-2.7 wxpython subprocess python-multithreading

有人告诉我将此作为一个新问题发布。这是一个跟进 Instantiating a new WX Python GUI from spawn thread

我将以下代码实现到从生成的线程(Thread2)

调用的脚本中
# Function that gets invoked by Thread #2
def scriptFunction():
  # Code to instantiate GUI2; GUI2 contains wx.TextCtrl fields and a 'Done' button
  p = subprocess.Popen("python secondGui.py", bufsize=2048, shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE)

  # Wait for a response
  p.wait()

  # Read response
  response = p.stdout.read()

  # Process entered data
  processData()

在运行GUI2的新进程中,我希望'Done'按钮事件处理程序将4个数据集返回给Thread2,然后自行销毁(GUI2)

def onDone(self,event):
  # This is the part I need help with; Trying to return data back to main process that instantiated this GUI (GUI2)
  process = subprocess.Popen(['python', 'MainGui.py'], shell=False, stdout=subprocess.PIPE)
  print process.communicate('input1', 'input2', 'input3', 'input4')

  # kill GUI
  self.Close()

目前,此实现在新进程中生成另一个主GUI。我想要做的是将数据返回到原始进程。感谢。

2 个答案:

答案 0 :(得分:0)

这两个脚本必须分开吗?我的意思是,您可以在一个主循环上运行多个帧,并使用pubsub在两者之间传输信息:http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

理论上,你所做的也应该奏效。我听说过的其他方法涉及使用Python的套接字服务器库来创建一个非常简单的服务器,该服务器运行两个程序可以发布和读取数据。或数据库或观看目录以进行文件更新。

答案 1 :(得分:0)

线程#2调用的函数

def scriptFunction():
  # Code to instantiate GUI2; GUI2 contains wx.TextCtrl fields and a 'Done' button
  p = subprocess.Popen("python secondGui.py", bufsize=2048, shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE)

  # Wait for a response
  p.wait()

  # Read response and split the return string that contains 4 word separated by a comma
  responseArray = string.split(p.stdout.read(), ",")

  # Process entered data
  processData(responseArray)
在GUI2上单击“完成”按钮时调用的“完成”按钮事件处理程序

def onDone(self,event):
  # Package 4 word inputs into string to return back to main process (Thread2)
  sys.stdout.write("%s,%s,%s,%s" % (dataInput1, dataInput2, dataInput3, dataInput4))

  # kill GUI2
  self.Close()

感谢您的帮助Mike!