首页 > 解决方案 > 如何在python中为for循环应用多线程

问题描述

我是 python 新手,我的代码中的一个命令需要很长时间才能执行,我需要根据列表多次执行它。我想在这里实现多线程。在下面的代码中 getcmdresult 需要很长时间才能返回一个值.有人可以建议我如何使用多线程来减少时间。提前致谢。

def getcmdresult(cmd, query):
    #process to return a json array
    return json_array


def get_details(list, query):
    resultlist=[]

    for item in list:
        cmd = "cmd "+item["name"]
        value=getcmdresult(cmd, query)
        resultlist.append(value)

    return resultlist

标签: pythonpython-3.xmultithreading

解决方案


推荐阅读