python - 如何在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
解决方案
推荐阅读
- html - 如何使内容滚动条使用主视口滚动条
- github - 如何在 GitHub 页面上发布 Ionic 5 网站?
- java - 获取 NoInitialContextException 尝试连接到 MySql 数据库
- treeview - TreeView 作为垂直菜单 - 如何获取关联 MenuItem 的名称
- javascript - 单击带有模板行的 v-data-table 中的行
- css - Rails 应用程序不会自动更新 CSS 中的更改
- java - Java Streams:获取列表中一组字符的总和
- python - 数据库或数据结构在某些条件下有效地找到最近的邻居
- html - 使用 Dinky 主题更改 GitHub 页面的帖子/页面内容的宽度
- postgresql - 使用 Ecto Fragments 将时间间隔添加到时间戳