简体   繁体   中英

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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