繁体   English   中英

多次在Windows shell中运行python脚本

[英]Running a python script in a windows shell multiple times

我想运行以下shell命令10次

./file.py 1111x

'x'的范围从0到9

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

那这个呢...

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)

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

[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 $_
}

暂无
暂无

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

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