python3中的多任务管理

jjhzyzn0  于 2023-05-19  发布在  Python
关注(0)|答案(2)|浏览(118)

在这里,我需要在两个步骤中运行许多子任务:
1.同时运行多个程序。这些程序是一些外部可执行程序。
1.等待步骤1中的所有程序结束后再运行下一个函数
我知道multipleprocessing会有帮助。但如何实施这两个步骤呢?
怎么挂主线?如何真实的查看子程序状态?
谢谢

xoefb8l8

xoefb8l81#

在阶段2上,仅存在具有不同参数的进程_3。使用*threading.Thread*对象的args参数,可以将参数传递给作业的函数。

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 ... ')
zsbz8rwp

zsbz8rwp2#

你可以使用threading包来做线程你的动作,你可以使用os包来运行一个可执行文件,这里是一个例子:

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 ... ')

这是多线程而不是多处理,但我希望它能为你做好这项工作,如果你对它们之间的区别有疑问,你可以看看这个link

相关问题