蟒蛇 |使用列表元素对方法进行多重处理

发布于 2025-01-11 17:59:51 字数 1186 浏览 0 评论 0原文

我正在尝试使用列表元素对方法进行多重处理。

每次,我都需要传递一组参数以及新的列表项。 我尝试了下面的代码,但无法实现工作并行性。

乐趣(列表元素,arg1,arg2)

from multiprocessing import Process
from multiprocessing import Pool

def fun(each_item, arg1, arg2):
        
    print('start fun')

    print("fun in this function")
    
    print('end fun')

def main():

    compute_HostList = ['abc.xyz.com', 'def.xyz.com', 'mno.xyz.com']

    #Given list
    print("Given Compute list: ",compute_HostList)
    
    # Each element as list
    New_OS_Compute_List= [[x] for x in compute_HostList]

    # Print
    print("The new lists of lists: ",New_OS_Compute_List)

    for each_item in NewList:
        print("item_in_list:", each_item)
        p = Process(target=fun, args=(each_item, arg1, arg2))
        p.start()
        p.join()

    """
    #I also tried this using zip, but not working.
    for every in New_OS_Compute_List:
        tasks = [*zip(every, "OS DBAAS", "dbcs_patching")]
        with Pool(5) as pool:
            pool.starmap(decideTypeOfPatch, iterable=tasks)
    """
            
if __name__ == '__main__':

    print('start main')
    main()
    print('end main')

I am trying to multiprocess a method using list elements.

Every time, I need to pass group of arguments along with new list item.
I tried below code, but not able to achieve working parallelism.

fun(list_element,arg1,arg2)

from multiprocessing import Process
from multiprocessing import Pool

def fun(each_item, arg1, arg2):
        
    print('start fun')

    print("fun in this function")
    
    print('end fun')

def main():

    compute_HostList = ['abc.xyz.com', 'def.xyz.com', 'mno.xyz.com']

    #Given list
    print("Given Compute list: ",compute_HostList)
    
    # Each element as list
    New_OS_Compute_List= [[x] for x in compute_HostList]

    # Print
    print("The new lists of lists: ",New_OS_Compute_List)

    for each_item in NewList:
        print("item_in_list:", each_item)
        p = Process(target=fun, args=(each_item, arg1, arg2))
        p.start()
        p.join()

    """
    #I also tried this using zip, but not working.
    for every in New_OS_Compute_List:
        tasks = [*zip(every, "OS DBAAS", "dbcs_patching")]
        with Pool(5) as pool:
            pool.starmap(decideTypeOfPatch, iterable=tasks)
    """
            
if __name__ == '__main__':

    print('start main')
    main()
    print('end main')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2025-01-18 17:59:51

当您执行 p.start() 后立即执行 p.join() 时,它会执行您告诉它执行的操作;-) 也就是说,它运行该进程,然后就坐在那里等待该过程完成。所以你没有得到有用的并行性。

因此,不要

        p = Process(target=fun, args=(each_item, arg1, arg2))
        p.start()
        p.join()

将流程对象保存在列表中:

    plist = []
    ...
        p = Process(target=fun, args=(each_item, arg1, arg2))
        plist.append(p)
        p.start()

并且循环结束后等待它们结束:

    for p in plist:
        p.join()

When you do p.start() immediately followed by p.join(), it does what you told it to do ;-) That is, it runs the process, and then just sits there waiting for the process to finish. So you get no useful parallelism.

So instead of

        p = Process(target=fun, args=(each_item, arg1, arg2))
        p.start()
        p.join()

save the process objects in a list instead:

    plist = []
    ...
        p = Process(target=fun, args=(each_item, arg1, arg2))
        plist.append(p)
        p.start()

and after that loop is over wait for them to end:

    for p in plist:
        p.join()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文