Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

python 3.x - How to execute mutiple commands concurrently in python3.6

I have two python scripts script1.py and script2.py. While running the script, each script has more than 5 arguments in form of .csv, .txt etc . 'argparse' is used when the script is ran individually to get the arguments. How can I execute the two commands concurrently in python3.6. Say the first command is 'script1.py -i file1.csv -t file2.txt' and second is 'script2.py -i file3.csv -t file4.txt". By running concurrently I wanted to reduce the run time. Following is what I have tried.

    import subprocess
    import time
    start_time = time.time()
    subprocess.run("script1.py -i file1.csv -t file2.txt & script2.py -i file3.csv -t file4.txt", shell = True)
print("Time in seconds %s " % (time.time() - start_time))

file1.csv and file3.csv are unequal split of file_x.csv. Since I am concerned about the execution time for data file with split and without split, I tried same script with single data (merged data file) file "file_x.csv" to see if execution time in both cases. To my surprise merged one (file_x.csv) took lesser time than the split one. So can someone help me to figure out the right way to run the code concurrently.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...