Python 2.7 将数据返回到调用子流程的原始流程

Python 2.7 将数据返回到调用子流程的原始流程,python-2.7,wxpython,subprocess,python-multithreading,Python 2.7,Wxpython,Subprocess,Python Multithreading,有人让我把这当作一个新问题来发帖。这是一个后续行动 我将以下代码实现到一个脚本中,该脚本从派生线程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

有人让我把这当作一个新问题来发帖。这是一个后续行动

我将以下代码实现到一个脚本中,该脚本从派生线程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。我想做的是将数据返回到原始流程。谢谢。

这两个脚本必须分开吗?我的意思是,您可以在一个主循环上运行多个帧,并使用pubsub在两个主循环之间传输信息:


从理论上讲,你所做的也应该起作用。我听说过的其他方法包括使用Python的socket服务器库创建一个非常简单的服务器,运行这两个程序可以发布和读取数据。或数据库,或查看文件更新的目录

线程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)
在GUI 2上单击“完成”按钮时调用的“完成”按钮事件处理程序

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

谢谢你的帮助,迈克

你是说数据没有返回到调用线程,还是说你不能让线程本身将数据传递回来?显然,因为我是新手,我无法发布我找到的解决方案。我将在稍后发布我的完整解决方案,一旦require8hrs等待时间过去,但这里是我快速而肮脏的实现。它不是最优雅的,但它满足了我的需要。线程2调用的函数有字符串解析器来分解串联的4数据集响应。事件处理程序将这4个字打包成一个字符串,返回到主进程Thread2 sys.stdout.write'input1'++'input2'++'input3'++'input4'如果其他人有更干净的方法,我仍然愿意接受建议。谢谢。您可以使用字符串替换%s%s%s%s%input1…input4或使用类似pickle的东西。谢谢,迈克,我来看看。我没有用那种方式实现它。我在一个回复帖子上工作,但由于我是新来的,所以被要求等待8小时的限制所阻止。我将研究它,并发布我目前使用的解决方案,以便其他人可以查看它。