在后台运行python脚本

时间:2017-05-11 12:57:47

标签: python

我编写了一个Python脚本来运行几个具有不同种子和不同节点数的NS-3模拟。 我的脚本调用./waf --run=<scenario_name>然后执行10个种子,更改节点数并再执行10个种子,依此类推。

问题是我调用脚本后,我要求用户输入(要运行的场景)。由于raw_input电话,我无法使用nohup myScript.py &。我还尝试了CTRL + Zbgdisown。但那也没有用。

这是我的剧本:

#!/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 个答案:

答案 0 :(得分:0)

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

p = subprocess.Popen(myArgument)

如果shell=True不需要,请避免使用ns3Global

  

Python 3.6»文档» 17.5. subprocess — Subprocess
  在新流程中执行子程序。

相关问题