5

I would like to run two python scripts at the same time on my lap top without any decreasing in their calculation's speed.

I have searched and saw this question saying that we should use bash file. I have searched but I did not understand what should I do and how to run those scrips with this way called bash.

python script1.py &
python script2.py &

I am inexperienced in it and I need your professional advice. I do not understand how to do that, where and how. I am using Windows 64bit.

Best

PS: The answer I checked the mark is a way to run in parallel two tasks, but it does not decrease the calculation time for two parallel tasks at all.

9
  • What is your operating system? Commented Feb 28, 2019 at 11:31
  • open a terminal and run the command python script1.py & python script2.py & ( ubuntu /linux based system ) you will be able to run the script without affecting on other performance Commented Feb 28, 2019 at 11:32
  • @quamrana windows Commented Feb 28, 2019 at 11:32
  • @prashantrana I am using windows. so just needs to write python script1.py & python script2.py & in command prompt? and every thing will be right? Commented Feb 28, 2019 at 11:33
  • 1
    @MaY yeah it will do it , what i meant was run python script1.py & click enter and then run python script2.py & in terminal Commented Feb 28, 2019 at 11:38

3 Answers 3

2

If you can install GNU Parallel on Windows under Git Bash (ref), then you can run the two scripts on separate CPUs this way:

▶ (cat <<EOF) | parallel --jobs 2
python script1.py
python script2.py
EOF

Note from the parallel man page:

   --jobs N
       Number of jobslots on each machine. Run up to N jobs in parallel.
       0 means as many as possible. Default is 100% which will run one job per
       CPU on each machine.

Note that the question has been updated to state that parallelisation does not improve calculation time, which is not generally a correct statement.

While the benefits of parallelisation are highly machine- and workload-dependent, parallelisation significantly improves the processing time of CPU-bound processes on multi-core computers.

Here is a demonstration based on calculating 50,000 digits of Pi using Spigot's algorithm (code) on my quad-core MacBook Pro:

Single task (52s):

▶ time python3 spigot.py
...
python3 spigot.py 52.73s user 0.32s system 98% cpu 53.857 total

Running the same computation twice in GNU parallel (74s):

▶ (cat <<EOF) | time parallel --jobs 2                                                                                                                                   
python3 spigot.py                                                                                                                                                      
python3 spigot.py                                                                                                                                                      
EOF        
...
parallel --jobs 2  74.19s user 0.48s system 196% cpu 37.923 total

Of course this is on a system that is busy running an operating system and all my other apps, so it doesn't halve the processing time, but it is a big improvement all the same.

See also this related Stack Overflow answer.

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you, I have git-bash on my window. I have this one in git bash command windows YMa@Yong21-PC MINGW64 ~ $ so shoul I write your code after $?
what is ▶ cat and etc. sorry for such a questions.
and could I ask you how to install gnu parallel on windoes?
The ▶ is my prompt. Ignore that. Install Git Bash, and then type everything exactly as you see it in my answer, with the ▶ character. As for installing Git Bash, did you follow the link to the related Stack Overflow question where there are instructions?
no because it does not open for me. I just had git bash before. but I do not know how to insall gnu plot using that
|
2

I use a batch file which contains these lines:

start python script1.py
start python script2.py

This opens a new window for each start statement.

5 Comments

won't call python script1.py & be better choice
Doesn't work for me. The two scripts run sequentially, not concurrently.
I have two question here. should I write it on command prompt . and another one this run both them in parallel? I mean if you open just two Python scripts with IDL your running time will increase. But I do not want to have longer time. Is it possible?
You can open two command prompts yourself and run script1 in one and script2 in the other, or, type the above lines in one command prompt, or use a batch file to start them.
So far so good. The let me finish my scripts that I have ran them with IDLE, I think they will be finished within 6 hours. Then I will run it. Each of them lasts for 21 hours, and when I run them both together the time is 26 hours. so I should try to see is it possible to have that 21 hours with this way or not.
1

A quite easy way to run parallel jobs of every kind is using nohup. This redirect the output to a file call nohup.out (by default). In your case you should just write:

nohup python script1.py > output_script1 &
nohup python script2.py > output_script2 &

That's it. With nohup you can also logout and the script will be continuing until they have finished

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.