我已經知道有幾個問題針對這個主題,但是他們都沒有解決我的具體問題。或者至少我找不到它。運行並從後臺進程獲得輸出
我需要在後臺執行一些程序,等待輸出並對其進行處理。 但後臺程序必須繼續執行。
來自我需要的後臺程序的信息恰好位於其輸出的第二行。如果此程序阻止我的代碼直到到達此行,則不會有任何問題。但重要的是它在該行之後解鎖,以便我可以執行與後臺程序完全無關的其他任務。
不過,我不知道如何做到這一點。我已經閱讀了很多subprocess
模塊的文檔,特別是subprocess.Popen
。
作爲實踐:爲什麼this code不適用於['localtunnel', '8000']
論點?它不輸出任何...
我知道我不需要root權限來執行此操作。從jadkik94答案後
編輯和巨星
遺憾的是,答案並不爲我工作。也許我做錯了什麼...
首先。 A「健全檢查」:
Main thread...
PING google.com (74.125.234.160) 56(84) bytes of data.
64 bytes from plusone.google.com (74.125.234.160): icmp_req=1 ttl=54 time=82.5 ms
Main thread...
64 bytes from plusone.google.com (74.125.234.160): icmp_req=2 ttl=54 time=82.7 ms
[...]
但是,當我與args
我想(args = ['localtunnel', 8000]
)使用它,唯一的輸出是Main thread...
:
import subprocess, threading, time
can_break = False
def run():
args = ["ping", "google.com"]
popen = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE)
while not can_break:
print popen.stdout.readline()
t = threading.Thread(target=run)
try:
t.start()
while True:
print 'Main thread...'
time.sleep(1)
except KeyboardInterrupt:
can_break = True
上面的代碼類似於此輸出工作正常。
當我在主線程(阻塞)調用localtunnel
,它返回所需的輸出:
In [x]: popen = subprocess.Popen(['localtunnel', '8000'])
This localtunnel service is brought to you by Twilio.
Port 8000 is now publicly accessible from http://????.localtunnel.com ...
這種方法是基於jadkik94的答案。但是,費斯特的答案也不起作用。
需要明確的是:在Python腳本是否需要任何後臺程序的輸出超過第二行?還是隻需要讓它繼續運行? –
第二行後,後臺程序可以「隱藏」其輸出。 – borges