简体   繁体   English

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

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

Let's say I have a program like this with a for loop, how could I go about making it multi-threaded using the threading module?假设我有这样一个带有 for 循环的程序,我如何使用线程模块使其成为多线程程序? I have tried, but the threads would overlap.我试过了,但线程会重叠。 For example, if i had 10 threads, it would send the post request 10 times for each word in the word list.例如,如果我有 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())

Assuming that you basically want to run requests in parallel without explicit need for multithreading:假设您基本上希望并行运行请求而不显式需要多线程:

Instead of awaiting the post request directly you could wrap it into a task and put this task into a queue.您可以将其包装成一个任务并将该任务放入队列中,而不是直接等待发布请求。 At the end of the main function you add a while loop that takes the tasks from the queue and awaits them (Most tasks are probably already completed at that point).在 main 函数的末尾添加一个 while 循环,该循环从队列中取出任务并等待它们(大多数任务可能在那时已经完成)。 Make sure to define an event that breaks your while loop when the queue is empty确保定义一个在队列为空时中断 while 循环的事件

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

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