在windows shell中多次运行python脚本

2024-04-27 00:27:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想运行下面的shell命令10次

./file.py 1111x

“x”在0到9之间

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


Tags: 文件实例端口py命令脚本windowsshell
2条回答

这个呢。。。在

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

相关问题 更多 >