简体   繁体   中英

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? 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.

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). Make sure to define an event that breaks your while loop when the queue is empty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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