Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running two python scripts with bash file

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.

like image 205
Ma Y Avatar asked Feb 28 '19 11:02

Ma Y


People also ask

How do I run multiple python scripts bash?

All you have to do is create a bash script(triple. bash) and write the commands for executing those three python scripts. And run this scripts using ./triple.

Can I run 2 python scripts at the same time?

Yes, we can run multiple python scripts at once. One can run multiple instances of IDLE/Python shell at the same time. In python, we use multi-threading to run multiple works simultaneously.

Can you run two bash scripts at the same time?

It can be used to run different commands together. The OR operator runs two commands one after the other, but the second command only gets executed when the first command finishes with an error. The second script gets executed only when the first script encounters an error.


2 Answers

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.

like image 64
Alex Harvey Avatar answered Oct 22 '22 12:10

Alex Harvey


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.

like image 31
quamrana Avatar answered Oct 22 '22 13:10

quamrana