2017-05-11 182 views
1

我編寫了一個Python腳本,用不同的種子和不同的節點數運行多個NS-3仿真。 我的腳本調用./waf --run=<scenario_name>然後執行10個種子,更改節點數並執行10個種子,依此類推。在後臺運行python腳本

問題是,我打電話給我的腳本後,我要求用戶輸入(運行哪個場景)。因爲那raw_input電話,我不能使用nohup myScript.py &。我也試過CTRL + Z,bgdisown。但是那也行不通。

這裏是我的腳本:

#!/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) 

任何幫助將非常感激。

+1

爲什麼不寫2個腳本?第一個腳本會詢問用戶輸入,然後在後臺啓動第二個腳本(使用scenario參數)並終止。 –

回答

0

使用subprocess.Popen(...代替subprocess.call(

p = subprocess.Popen(myArgument) 

避免使用shell=True如果ns3Global不需要它。

的Python 3.6»文檔»17.5. subprocess — Subprocess
在新進程中執行一個子程序。

+0

我做了什麼指示說有關使用'Popen'和解析命令作爲列表,但得到了[[Errno 2]沒有這樣的文件或目錄' – John

+0

你看過關於shlex.split()**的**注意事項**你在鏈接頁面上看到下面的內容? – stovfl

+0

是的。我使用'args = shlex.split(myArgument)'然後'p = subprocess.Popen(args)' – John