I have a python script, it invokes another python file and runs it. However I need the script to run the same file in parallel for multiple times. I will share the code snippet here .This runs the python file once.
output = os.popen('python py_generator_sm20.py' + options)
print output.read()
How do I parallelize it to run multiple times simultaneously?
This maybe not full answer you, because of que
Using the output
part of your code, but could be a start point.
multiprocessing
module you can create a pool of workers and then with the subprocess
module you can call one instance of your script for each worker and check the output :
import multiprocessing as mp
import subprocess as sp
# an example with two runs
commands = ['python test.py', 'python test.py']
# pass the number of threads that will be working
# if the number of threads < len(commands) the exceed
# will run in sequence when some process terminate
pool = mp.Pool(processes=2)
# execute the script calls
res = pool.map(sp.check_output, commands)
print(*[item.decode() for item in res])
pool.close()
Attention: the return from check_output
is a byte string
, so you need to convert it back to string
I tested it with the following simple program:
import time
if __name__ == "__main__":
print("Running an instance at {}".format(time.ctime()))
time.sleep(2)
print("Finished at {}".format(time.ctime()))
And that is the output:
Running an instance at Thu Oct 11 23:21:44 2018
Finished at Thu Oct 11 23:21:46 2018
Running an instance at Thu Oct 11 23:21:44 2018
Finished at Thu Oct 11 23:21:46 2018
As you can see they runned at same time.
I think you need this:
from multiprocessing.dummy import Pool as ThreadPool
pt = ThreadPool(4)
results = pt.map(pt_function, pt_array)
or maybe this way (if you have many threading scripts):
from Threading_orders import Thread
class First_time(Thread):
"""
A threading example
"""
def __init__(self, a, b):
"""Инициализация потока"""
Thread.__init__(self)
self.a = a
self.b = b
def run(self):
MyThread_1(self.a, self.b)
MyThread_2(self.a, self.b)
MyThread_3(self.a, self.b)
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.