简体   繁体   English

在后台运行python脚本

[英]run python script in the background

I wrote a Python script to run several NS-3 simulations with different seeds and different node counts. 我编写了一个Python脚本来运行几个具有不同种子和不同节点数的NS-3模拟。 My script calls ./waf --run=<scenario_name> then executes 10 seeds, change node count and execute 10 more seeds, and so on. 我的脚本调用./waf --run=<scenario_name>然后执行10个种子,更改节点数并再执行10个种子,依此类推。

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 & . 由于raw_input调用,我无法使用nohup myScript.py & I also tried CTRL + Z , bg , and disown . 我也尝试过CTRL + Zbgdisown 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( 使用subprocess.Popen(...而不是subprocess.call(

p = subprocess.Popen(myArgument)

Avoid to use shell=True if ns3Global don't need it. 如果ns3Global不需要,请避免使用shell=True

Python 3.6 » Documentation » 17.5. Python 3.6»文档» 17.5。 subprocess — Subprocess subprocess - 子流程
Execute a child program in a new process. 在新进程中执行子程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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