當使用Python的sh模塊(未STDLIB的一部分),我可以在我的道路調用程序的功能,並在後臺運行它:Python的SH模塊 - 在後臺運行命令封套
from sh import sleep
# doesn't block
p = sleep(3, _bg=True)
print("prints immediately!")
p.wait()
print("...and 3 seconds later")
我可以使用SH的Command
包裝和可執行文件的絕對路徑傳遞(有益的,如果可執行文件是不是在我的路徑或具有字符,如.
):
import sh
run = sh.Command("/home/amoffat/run.sh")
run()
但試圖運行包可執行文件中背景,作爲foll OWS:
import sh
run = sh.Command("/home/amoffat/run.sh", _bg=True)
run()
有回溯錯誤失敗:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument '_bg'
如何運行通過sh.Command
在後臺包裹可執行?尋找一個優雅的解決方案。
編輯:
我用Python解釋用於測試傳遞_bg
到命令(未包裝),我現在知道的是一個壞的方式來測試用於阻塞和非阻塞的過程:
>>> import sh
>>> hello = sh.Command("./hello.py")
>>> hello(_bg=True) # 5 second delay before the following prints and prompt is returned
HI
HI
HI
HI
HI
隨着hello.py爲如下:
#!/usr/bin/python
import time
for i in xrange(5):
time.sleep(1)
print "HI"
你可以使用POPEN /子。 – Claris