将数据返回到调用子进程的原始进程 [英] Returning data to the original process that invoke a subprocess

查看:43
本文介绍了将数据返回到调用子进程的原始进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我将此作为一个新问题发布.这是一个跟进从 spawn 线程实例化一个新的 WX Python GUI

Someone told me to post this as a new question. This is a follow up to Instantiating a new WX Python GUI from spawn thread

我将以下代码实现到从衍生线程 (Thread2) 调用的脚本中

I implemented the following code to a script that gets called from a spawned 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 的新进程上,我希望完成"按钮事件处理程序向 Thread2 返回 4 个数据集,然后销毁自身(GUI2)

On the new process running GUI2, I want the 'Done' button event handler to return 4 data sets to Thread2, and then destroy itself (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.我想要做的是将数据返回到原始进程.谢谢.

Currently, this implementation spawns another Main GUI in a new process. What I want to do is return data back to the original process. Thanks.

推荐答案

被线程 #2 调用的函数

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 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 上单击完成"按钮时调用的完成"按钮事件处理程序

'Done' button event handler that gets invoked when the 'Done' button is clicked on 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()

感谢迈克的帮助!

这篇关于将数据返回到调用子进程的原始进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆