简体   繁体   English

同时运行多个 python 脚本

[英]Run multiple python scripts concurrently

How can I run multiple python scripts?如何运行多个 python 脚本? At the moment I run one like so python script1.py .目前我运行一个这样的python script1.py

I've tried python script1.py script2.py and that doesn't work: only the first script is run.我试过python script1.py script2.py不起作用:只运行了第一个脚本。 Also, I've tried using a single file like this;另外,我试过使用这样的单个文件;

import script1
import script2

python script1.py
python script2.py

However this doesn't work either.但是,这也不起作用。

With Bash:使用 Bash:

python script1.py &
python script2.py &

That's the entire script.这就是整个脚本。 It will run the two Python scripts at the same time.它将同时运行两个 Python 脚本。

Python could do the same thing itself but it would take a lot more typing and is a bad choice for the problem at hand. Python 本身可以做同样的事情,但它需要更多的输入,对于手头的问题来说是一个糟糕的选择。

I think it's possible though that you are taking the wrong approach to solving your problem, and I'd like to hear what you're getting at.我认为虽然您采取了错误的方法来解决您的问题,但我想听听您的意见。

The simplest solution to run two Python processes concurrently is to run them from a bash file, and tell each process to go into the background with the & shell operator.同时运行两个 Python 进程的最简单解决方案是从 bash 文件运行它们,并使用& shell 运算符告诉每个进程进入后台。

python script1.py &
python script2.py &

For a more controlled way to run many processes in parallel, look into the Supervisor project , or use the multiprocessing module to orchestrate from inside Python.要以更可控的方式并行运行多个进程,请查看Supervisor 项目,或使用多处理模块从 Python 内部进行编排。

I had to do this and used subprocess.我必须这样做并使用子流程。

import subprocess

subprocess.run("python3 script1.py & python3 script2.py", shell=True)

I do this in node.js (on Windows 10) by opening 2 separate cmd instances and running each program in each instance.我在 node.js(在 Windows 10 上)中通过打开 2 个单独的 cmd 实例并在每个实例中运行每个程序来执行此操作。

This has the advantage that writing to the console is easily visible for each script.这样做的优点是可以很容易地看到每个脚本对控制台的写入。

I see that in python can do the same: 2 shells.我看到在 python 中可以做同样的事情:2 个 shell。

You can run multiple instances of IDLE/Python shell at the same time.您可以同时运行多个 IDLE/Python shell 实例。 So open IDLE and run the server code and then open up IDLE again, which will start a separate instance and then run your client code.因此,打开 IDLE 并运行服务器代码,然后再次打开 IDLE,这将启动一个单独的实例,然后运行您的客户端代码。

you can use您可以使用

import os
os.system("script1.py && script2.py")

and so on you can add ou as many scripts as you want: just separate with &&依此类推,您可以根据需要添加任意数量的脚本:只需用&&分隔

This is for Windows OS这适用于 Windows 操作系统

I am working in Windows 7 with Python IDLE.我正在使用 Python IDLE 在 Windows 7 中工作。 I have two programs,我有两个程序,

# progA
while True:
    m = input('progA is running ')
    print (m)

and

# progB
while True:
    m = input('progB is running ')
    print (m)

I open up IDLE and then open file progA.py.我打开 IDLE,然后打开文件 progA.py。 I run the program, and when prompted for input I enter "b" + <Enter> and then "c" + <Enter>我运行程序,当提示输入时,我输入"b" + <Enter>然后"b" + <Enter> "c" + <Enter>

I am looking at this window:我正在看这个窗口:

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\progA.py =
progA is running b
b
progA is running c
c
progA is running 

Next, I go back to Windows Start and open up IDLE again, this time opening file progB.py.接下来,我回到 Windows 开始并再次打开 IDLE,这次打开文件 progB.py。 I run the program, and when prompted for input I enter "x" + <Enter> and then "y" + <Enter>我运行程序,当提示输入时,我输入"x" + <Enter> ,然后"x" + <Enter> "y" + <Enter>

I am looking at this window:我正在看这个窗口:

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\progB.py =
progB is running x
x
progB is running y
y
progB is running 

Now two IDLE Python 3.6.3 Shell programs are running at the same time, one shell running progA while the other one is running progB.现在两个 IDLE Python 3.6.3 Shell 程序同时运行,一个 shell 运行 progA 而另一个运行 progB。

您可以使用Gnu-Parallel并发运行命令,适用于 Windows、Linux/Unix。

parallel ::: "python script1.py" "python script2.py"

Adding a wait at the end of the bash script would be advisable, as the script exits when one of the process completes.建议在 bash 脚本的末尾添加一个wait ,因为脚本会在其中一个进程完成时退出。 If we need the script to exit only after all the python process are completed, add a wait at the end of the bash script.如果我们需要脚本在所有python进程完成后才退出, wait在bash脚本末尾添加一个wait

So the script would be所以脚本将是

#!/bin/bash
python script1.py ;
python script2.py &
python script3.py &
wait

; at the end of first script is used to run script1 & once finished then start with script2 & script3 in parallel and wait till all of the 3 scripts are completed.在第一个脚本的末尾用于运行 script1 & 一旦完成,然后并行启动 script2 & script3 并等待所有 3 个脚本都完成。

If you want to run two Python scripts in parallel then just include the following at the end of your script: 如果要并行运行两个Python脚本,则只需在脚本末尾包含以下内容:

if __name__=="__main__":
     Main()

The most simple way in my opinion would be to use the PyCharm IDE and install the 'multirun' plugin.我认为最简单的方法是使用 PyCharm IDE 并安装“multirun”插件。 I tried alot of the solutions here but this one worked for me in the end!我在这里尝试了很多解决方案,但最终这个解决方案对我有用!

You try the following ways to run the multiple python scripts:您可以尝试以下方法来运行多个 python 脚本:

  import os
  print "Starting script1"
  os.system("python script1.py arg1 arg2 arg3")
  print "script1 ended"
  print "Starting script2"
  os.system("python script2.py arg1 arg2 arg3")
  print "script2 ended"

Note: The execution of multiple scripts depends purely underlined operating system, and it won't be concurrent, I was new comer in Python when I answered it.注意:多个脚本的执行纯粹依赖于带下划线的操作系统,不会并发,我回答的时候是Python新手。

Update: I found a package: https://pypi.org/project/schedule/ Above package can be used to run multiple scripts and function, please check this and maybe on weekend will provide some example too.更新:我找到了一个包: https : //pypi.org/project/schedule/上面的包可以用来运行多个脚本和功能,请检查这个,也许周末也会提供一些例子。

ie: IE:

 import schedule
 import time
 import script1, script2

 def job():
     print("I'm working...")

 schedule.every(10).minutes.do(job)
 schedule.every().hour.do(job)
 schedule.every().day.at("10:30").do(job)
 schedule.every(5).to(10).days.do(job)
 schedule.every().monday.do(job)
 schedule.every().wednesday.at("13:15").do(job)

 while True:
     schedule.run_pending()
     time.sleep(1)

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

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