简体   繁体   中英

run python script in the background

I wrote a Python script to run several NS-3 simulations with different seeds and different node counts. My script calls ./waf --run=<scenario_name> then executes 10 seeds, change node count and execute 10 more seeds, and so on.

The problem is that after I call my script, I ask the user for an input (which scenario to run). Because of that raw_input call, I couldn't use nohup myScript.py & . I also tried CTRL + Z , bg , and disown . But that didn't work either.

Here's my script:

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

Any help will be much appreciated.

Use subprocess.Popen(... instead of subprocess.call(

p = subprocess.Popen(myArgument)

Avoid to use shell=True if ns3Global don't need it.

Python 3.6 » Documentation » 17.5. subprocess — Subprocess
Execute a child program in a new process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM