import threading
from time import sleep
import os
def process_1():
print('process_1 running')
# calling your executable
os.system("./process_1.sh")
# simultating the executable time
sleep(5)
def process_2():
print('process_2 running')
os.system("./process_2.sh")
sleep(5)
def process_3(parameter:str):
command_list = ("./process_3.sh", parameter)
command = " ".join(command_list)
print('executing : ', command)
os.system(command)
sleep(5)
if __name__ == '__main__':
# storing the processes functions into phase 1 and 2
phase_1_processes = [process_1, process_2]
phase_2_processes = [process_3]
process_3_parameters = ['param_1', 'param_2']
print('starting phase 1 ...')
# phase_1 ...
print('phase_1 is over ... ')
# phase 2, with only process_3
print('starting phase 2 ...')
jobs=[]
for param in process_3_parameters:
print('param inside run : ', param)
jobs.append(threading.Thread(target=process_3,
args=(param,))) # note the comma
# starting the jobs
for job in jobs:
job.start()
# Wait for all the thread to finish
for job in jobs:
job.join()
print('phase_2 is over ... ')
import threading
from time import sleep
import os
def process_1():
print('process_1 running')
# calling your executable
os.system("./path_to_process_1/process_1.sh")
# simultating the executable time
sleep(5)
def process_2():
print('process_2 running')
os.system("./path_to_process_2/process_2.sh")
sleep(5)
def process_3():
print('process_3 running')
os.system("./path_to_process_3/process_3.sh")
sleep(5)
def process_4():
print('process_4 running')
os.system('./path_to_process_4/process_4.sh')
sleep(5)
if __name__ == '__main__':
# storing the processes functions into phase 1 and 2
phase_1_processes = [process_1, process_2]
phase_2_processes = [process_3, process_4]
print('starting phase 1 ...')
# creating a job list in which we'll store the processes to be run
jobs=[]
for process in phase_1_processes:
jobs.append(threading.Thread(target=process))
# starting the jobs
for job in jobs:
job.start()
# Wait for all the thread to finish
for job in jobs:
job.join()
print('phase_1 is over ... ')
# phase 2
print('starting phase 2 ...')
jobs=[]
for process in phase_2_processes:
jobs.append(threading.Thread(target=process))
# starting the jobs
for job in jobs:
job.start()
# Wait for all the thread to finish
for job in jobs:
job.join()
print('phase_2 is over ... ')
2条答案
按热度按时间xoefb8l81#
在阶段2上,仅存在具有不同参数的进程_3。使用*threading.Thread*对象的args参数,可以将参数传递给作业的函数。
zsbz8rwp2#
你可以使用threading包来做线程你的动作,你可以使用os包来运行一个可执行文件,这里是一个例子:
这是多线程而不是多处理,但我希望它能为你做好这项工作,如果你对它们之间的区别有疑问,你可以看看这个link。