在Windows Shell中多次运行python脚本

时间:2019-05-12 17:12:39

标签: python powershell

我想运行以下shell命令10次

./file.py 1111x

“ x”的范围是0到9

即每个.file.py文件的不同端口。我需要每个实例都在自己的外壳中运行。我已经尝试创建一个批处理文件和一个调用Windows Shell的python脚本,但是都没有成功。

2 个答案:

答案 0 :(得分:0)

那呢...

import os
import subprocess 
for x in range(0,10):
    command = './file.py 1111'  + str(x)
    os.system(command)
    #or
    subprocess.call('cmd ' + command, shell=True)

答案 1 :(得分:0)

您正在寻找的是功能强大的工作。您可能需要稍微调整一下以适应您的特定要求,但这应该可以满足您的需求。

[ScriptBlock]$PyBlock = {
   param (
     [int]$x,
     [string]$pyfile
   )
   try {
     [int]$Port = (11110 + $x)
     python $pyfile $Port
   }
   catch {
     Write-Error $_
   }
}

try {
  0..9 | ForEach-Object {
    Start-Job -Name "PyJob $_" -ScriptBlock $PyBlock -ArgumentList @($_, 'path/to/file.py')
  }

  Get-Job | Wait-Job -Timeout <int> 
     #If you do not specify a timeout then it will wait indefinitely. 
     #If you use -Timeout then make sure it's long enough to accommodate the runtime of your script. 

  Get-Job | Receive-Job
}
catch {
  throw $_
}
相关问题