Run multiple python scripts at once

How can I run multiple python scripts? I am currently running one like python script1.py .

I tried python script1.py script2.py and this does not work: only the first script is launched. In addition, I tried using a single file as follows:

 import script1 import script2 python script1.py python script2.py 

However, this does not work either.

+22
source share
9 answers

With Bash:

 python script1.py & python script2.py & 

Whats all the script. It will run two Python scripts at the same time.

Python could do the same, but it would take a lot more text input and a poor choice for this problem.

I think this is possible, although you are taking the wrong approach to solving your problem, and I would like to hear what you get.

+41
source

The simplest solution to running two Python processes simultaneously is to start from a bash file and tell each process to go to the background using the shell operator & .

 python script1.py & python script2.py & 

For a more controlled way to run multiple processes at once, check out the Supervisor project or use the multiprocessing module to organize from within Python.

+17
source

I work on Windows 7 with Python IDLE. 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 IDLE and then open the progA.py file. I run the program, and when prompted for input, type "b" + <Enter> and then "c" + <Enter>

I look 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 

Then I go back to Windows Start and open IDLE again, this time opening the progB.py file. I run the program, and when prompted for input, type "x" + <Enter> and then "y" + <Enter>

I look 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 run at the same time, one shell starts progA and the other progB.

0
source

You can use Gnu-Parallel to run commands at the same time, it works on Windows, Linux / Unix.

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

0
source

I do this in node.js (on Windows 10), opening 2 separate cmd instances and running each program in each instance.

This has the advantage that writing to the console is easily visible for each script.

I see that in python you can do the same thing: 2 shells.

You can run multiple instances of the IDLE / Python shell at the same time. So, open IDLE and run the server code, and then open IDLE again, which will launch a separate instance and then run your client code.

0
source

If you want to run two Python scripts in parallel, simply add the following to the end of the script:

 if __name__=="__main__": Main() 
0
source

Try using dockers. Dockers are a good choice for separating different systems.

0
source

I had to do this and used a subprocess.

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

You try the following ways to run multiple Python scripts:

  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 several scripts depends on a purely underlined operating system, and it will not be parallel, I was new to Python when I answered it.

Update: I found the package: https://pypi.org/project/schedule/ The above package can be used to run several scripts and functions, please check this out and maybe on the weekend will also provide some example.

that is: import schedule import time 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) 
-2
source

Source: https://habr.com/ru/post/1496881/


All Articles