Home > Software design >  Running multiple async tasks in a for loop
Running multiple async tasks in a for loop

Time:11-07

I have two lists: queries and search_engines. I want to get each query and run a search on its corresponding search engine. I want to however do this concurrently in order to save some time. I am totally new to asynchronous programming and this is my attempt to do it. I keep getting an "RuntimeError: asyncio.run() cannot be called from a running event loop" error.

import asyncio
import time


queries = ['messi', 'ronaldo', 'iniesta', 'xavi', 'ramos']
engines = ['Google', 'Google', 'Bing', 'Duckduckgo', 'Google']
async def get_queries():
    for query, engine in zip(queries, engines):
        if engine == 'Google':
            gtask = asyncio.create_task((google_search.get_domain_description(query)))
            await asyncio.sleep(2)
        if engine == 'Bing':
            btask = asyncio.create_task(print(bing_search.get_domain_description(query)))
            await asyncio.sleep(2)
        if engine == 'Duckduckgo':
            dtask = asyncio.create_task(print(duckduckgo_search.get_domain_description(query)))
            await asyncio.sleep(2)

                                        
asyncio.run(get_queries())

CodePudding user response:

import asyncio

SEARCH_ENGINES = {
    'Google': google_search,
    'Bing': bing_search,
    'Duckduckgo': duckduckgo_search
}

async def get_query(engine, query):
    await SEARCH_ENGINES[engine].get_domain_description(query)

async def main():
    queries = ['messi', 'ronaldo', 'iniesta', 'xavi', 'ramos']
    engines = ['Google', 'Google', 'Bing', 'Duckduckgo', 'Google']
    await asyncio.gather(
        *[get_query(engine, query) for query, engine in zip(queries, engines)])

asyncio.run(main())
  • Related