繁体   English   中英

如何在 Python 的 for 循环中引入多线程?

[英]How can I introduce multi-threading into a for loop in Python?

假设我有这样一个带有 for 循环的程序,我如何使用线程模块使其成为多线程程序? 我试过了,但线程会重叠。 例如,如果我有 10 个线程,它将为单词列表中的每个单词发送 10 次 post 请求。

import aiohttp, asyncio

word_file = open("word.txt", "r")
word_list = word_file.readlines()
parsed_words = l = [i.strip() for i in word_list]

async def main():
    async with aiohttp.ClientSession() as session:
        for i in parsed_words:
            await session.post(f"https://redacted/{i}/")
            
asyncio.run(main())

假设您基本上希望并行运行请求而不显式需要多线程:

您可以将其包装成一个任务并将该任务放入队列中,而不是直接等待发布请求。 在 main 函数的末尾添加一个 while 循环,该循环从队列中取出任务并等待它们(大多数任务可能在那时已经完成)。 确保定义一个在队列为空时中断 while 循环的事件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM