多任务管理在Python 3中

huangapple go评论56阅读模式
英文:

multiple task management in python3

问题

这里我需要在2个步骤中运行多个子任务:

  1. 同时运行多个程序。这些程序是一些外部可执行程序。

  2. 等待步骤1中的所有程序结束后再运行下一个函数。

我知道multipleprocessing会有帮助。但如何实现这两个步骤?

如何挂起主线程?
如何实时检查子程序的状态?

谢谢。

英文:

Here I need to run many sub-tasks in 2 steps:

  1. run multiple programs at the same time. These programs are some external executable programs.

  2. Wait for all programs in step 1 to end before running the next function

I known the multipleprocessing will help. But how to implement these two steps?

How to hang up the main thread?
How to check the sub-programs status in real time?

Thanks.

答案1

得分: 0

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

import threading
from time import sleep
import os

def process_1():
    print('process_1 running')
    # 调用可执行文件
    os.system("./path_to_process_1/process_1.sh")
    # 模拟可执行文件运行时间
    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__':
    
    # 将进程函数存储到阶段1和2中
    phase_1_processes = [process_1, process_2]
    phase_2_processes = [process_3, process_4]
    
    print('starting phase 1 ...')
    # 创建一个作业列表,用于存储要运行的进程
    jobs=[]
    for process in phase_1_processes:
        jobs.append(threading.Thread(target=process))

    # 启动作业
    for job in jobs:
        job.start()

    # 等待所有线程完成
    for job in jobs:
        job.join()
    print('phase_1 is over ...')
    
    # 阶段2
    print('starting phase 2 ...')
    jobs=[]
    for process in phase_2_processes:
        jobs.append(threading.Thread(target=process))

    # 启动作业
    for job in jobs:
        job.start()

    # 等待所有线程完成
    for job in jobs:
        job.join()
    print('phase_2 is over ...')

这是多线程而不是多进程,但我希望这对你有帮助。如果你对它们之间的区别有疑问,你可以查看这个链接

英文:

You can use the threading package to do thread your actions, you can use the os package to run a executable file, here is an example :

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

This is multithreading and not multiprocessing, but I hope it will do the job for you, if you have a doubt on the difference between them you can look at this link.

答案2

得分: 0

在第2阶段,只有一个具有不同参数的process_3。使用threading.Thread对象的args参数,您可以将参数传递给您的作业函数。

import threading
from time import sleep
import os

def process_1():
    print('process_1 running')
    # 调用您的可执行文件
    os.system("./process_1.sh")
    # 模拟可执行文件的执行时间
    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__':
    
    # 将进程函数存储到阶段1和2中
    phase_1_processes = [process_1, process_2]
    phase_2_processes = [process_3]
    process_3_parameters = ['param_1', 'param_2']

    print('starting phase 1 ...')
    # 阶段1...
    print('phase_1 is over ... ')
    
    # 阶段2,只有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,))) # 注意逗号

    # 启动作业
    for job in jobs:
        job.start()

    # 等待所有线程完成
    for job in jobs:
        job.join()
    print('phase_2 is over ... ')
英文:

On phase 2 there is only process_3 with different parameters. Using the args argument of the threading.Thread object you can pass arguments to you job's function.

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

huangapple
  • 本文由 发表于 2023年5月17日 10:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定