Python запуск нескольких скриптов одновременно

Running two scripts in parallel in Pycharm

I have two scripts, server.py and client.py. I want to be able to start them running, in that order, with one action. How can achieve that in Pycharm? Please note that I want to be able to set breakpoints

3 Answers 3

  1. Run -> Edit Configurations.
  2. Find and add a new «Compound» project
  3. Add your configurations into a single component
  4. Run it

Run -> Edit Configurations. Edit your script configuration Check the «Allow parallel run» checkbox at the top

Then just run then normally. You can also pin run tab to not be closed

Allows to run multiple run configurations at once: group multiple run configurations and start them in a single click. Not only application and test run configurations can be grouped, but other Multirun configurations can be organized into single run configuration.

It will let you run all configurations in Debug mode and use breakpoints.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Run multiple python scripts concurrently

How can I run multiple python scripts? At the moment I run one like so python script1.py . I’ve tried python script1.py script2.py and that doesn’t work: only the first script is run. Also, I’ve tried using a single file like this;

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

Yeah. What platform? This is a very obvious example of something that should be done with a VERY simple bash script.

10 Answers 10

python script1.py & python script2.py & 

That’s the entire script. It will run the two Python scripts at the same time.

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.

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.

Is this is the best way to run multiple python scripts ? I mean should i write the python script to run other python script or directly write shell scipts ? Just wanted to know best practice ?

@ChristopherPeterson what is the recommended way to terminate them. I have a programs that run an infinite loop and just need stopping once in a while (once per two days roughly) and I dont have a convienient way to kill 20 scripts.

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 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.

Probably I posted this before or right after the accepted answer. I could turn the question around and ask why didn’t I get any upvotes.

Interestingly your answer seems to have been posted before the accepted answer. There’s a three minute difference.

@logc, +1, in this case the two python instances are completely isolated, correct? If one wanted to compare the output from script1.py to the output from script2.py , do you know the best method?

Adding a wait at the end of the bash script would be advisable, as the script exits when one of the process completes. 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.

#!/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.

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.

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.

You can run multiple instances of IDLE/Python shell at the same time. 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.

The most simple way in my opinion would be to use the PyCharm IDE and install the ‘multirun’ plugin. I tried alot of the solutions here but this one worked for me in the end!

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

and so on you can add ou as many scripts as you want: just separate with &&

You can use Gnu-Parallel to run commands concurrently, works on Windows, Linux/Unix.

parallel . «python script1.py» «python script2.py»

I am working in Windows 7 with Python IDLE. I have two programs,

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

I open up IDLE and then open file progA.py. I run the program, and when prompted for input I enter «b» + and then «c» +

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. I run the program, and when prompted for input I enter «x» + and then «y» +

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.

Источник

Читайте также:  All python colors list
Оцените статью