[英]Windows python script to run a server and continue
我正在使用python腳本啟動服務器,該服務器可能處於后台運行或處於其他進程中,然后在終止啟動的服務器之前進行進一步處理。
一旦其余的處理結束,則終止啟動的服務器。
例如
server_cmd = 'launch_server.exe -source '+ inputfile
print server_cmd
cmd_pid = subprocess.Popen(server_cmd).pid
...
...
... #Continue doing some processing
cmd_pid.terminate() # Once the processing is done, terminate the server
啟動服務器后腳本不會繼續運行的某些方式,因為服務器可能正在無限循環中運行以偵聽請求。 有沒有什么好方法可以在后台發送此進程,以免命令行輸入不期望它。
我正在使用Python 2.7.8
奇怪的是,啟動服務器命令后腳本無法繼續執行。 在subprocess
模塊中, Popen
啟動另一個子流程,而父流程(您的腳本)應繼續運行。
但是,在您的代碼中已經存在一個錯誤: cmd_pid
是一個int
對象,沒有terminate
方法。 您應該使用subprocess.Popen
對象來調用terminate
方法。
進行較小的更改即可解決問題
server_proc = subprocess.Popen(server_cmd, stdout = subprocess.PIPE )
server_proc.terminate()
感謝Xu對終止的更正。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.