[英]how to run a different process on a different thread and get the output to the first thread? python
我想從此腳本運行另一個python腳本(scriptA.py),我嘗試使用process = subprocess.popen(cmd, stdout=subprocess.PIPE)
subprocess.popen process = subprocess.popen(cmd, stdout=subprocess.PIPE)
,然后在輸出行上進行迭代。
並且還嘗試了此解決方案https://gist.github.com/kirpit/1306188
但還不確定如何實施
我想使用主文件(subprocess.popen等)中的代碼打開scriptA.py,但要在主文件中的另一個線程上打開它。.因為它們都需要並行處理。
scriptA.py將從android設備獲取信息。SM將根據scriptA.py的輸出向同一設備發出命令。
有任何想法嗎?
謝謝
默認情況下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:無限循環僅用於介紹概念
首先,我讀到: 進程和線程之間有什么區別?
您需要確定它是所需的線程還是進程。 如果您確實想要一個進程,則需要生成一個子進程,然后等待其結束並讀取輸出。 這可能會有所幫助: 等待流程直到所有子流程完成?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.