[英]run python script in the background
我編寫了一個Python腳本來運行幾個具有不同種子和不同節點數的NS-3模擬。 我的腳本調用./waf --run=<scenario_name>
然后執行10個種子,更改節點數並再執行10個種子,依此類推。
問題是,在我調用腳本后,我要求用戶輸入(要運行的場景)。 由於raw_input
調用,我無法使用nohup myScript.py &
。 我也嘗試過CTRL + Z
, bg
和disown
。 但這也不起作用。
這是我的腳本:
#!/usr/bin/python
import subprocess
from pathlib import Path
import glob
scenario = raw_input("Type scenario (foo or bar): ")
if scenario == 'foo':
wafString = './waf --run "scratch/test-foo --nodeCount='
elif scenario == 'bar':
wafString = './waf --run "scratch/test-bar --nodeCount='
else:
print ("Wrong input!")
ns3Global = 'NS_GLOBAL_VALUE="RngRun='
numbers = [25, 50, 100] # number of nodes
for nodeCount in numbers:
for rngRun in range(1,11):
myArgument = ns3Global + str(rngRun) + '" ' + wafString + str(nodeCount) + '" '
print "*** Running experiment with " + str(nodeCount) + \
" nodes and random seed " + str(rngRun) + "\n"
subprocess.call(myArgument, shell=True)
任何幫助都感激不盡。
使用subprocess.Popen(...
而不是subprocess.call(
p = subprocess.Popen(myArgument)
如果ns3Global
不需要,請避免使用shell=True
。
Python 3.6»文檔» 17.5。 subprocess - 子流程
在新進程中執行子程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.