【问题标题】:how to run a different process on a different thread and get the output to the first thread? python如何在不同的线程上运行不同的进程并将输出获取到第一个线程? Python
【发布时间】:2016-05-21 05:58:25
【问题描述】:

我想从这个脚本中运行另一个 python 脚本 (scriptA.py) 我试过了 process = subprocess.popen(cmd, stdout=subprocess.PIPE) 然后在输出行上迭代..

也尝试了这个解决方案 https://gist.github.com/kirpit/1306188

但不确定如何实现它..

我想使用主文件中的代码(subprocess.popen 等)打开 scriptA.py 但是要在主文件的另一个线程上打开它..因为他们都需要并行做事..

scriptA.py 将从安卓设备中提取信息。 SM会根据scriptA.py..的输出向同一个设备发出命令。

有什么想法吗?

谢谢

【问题讨论】:

    标签: android python multithreading


    【解决方案1】:

    首先我会读到: What is the difference between a process and a thread?

    您需要确定它是您需要的线程还是进程。 如果你真的想要一个进程,你需要生成一个子进程,然后等待它结束并读取输出。 这可能会有所帮助: wait process until all subprocess finish?

    【讨论】:

      【解决方案2】:

      默认情况下subprocess.Popen 在新进程中运行子程序。所以这根本不会阻塞你的主进程。

      childProc = subprocess.Popen(cmd ..)
      
      while True:
          childProc.poll() # to see if it has finished 
          if childProc.returncode:
              break # Finished
          # communicate somedata and read stdout and stderrs
          outs, errs = childProc.communicate(input="somedata")
      

      PS : 无限循环只是为了展示概念

      【讨论】:

        猜你喜欢
        • 2012-04-05
        • 2011-07-02
        • 1970-01-01
        • 2022-01-18
        • 1970-01-01
        • 2011-05-19
        • 2015-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多