Home > Blockchain >  Is there a faster/more efficient way than a loop to make multiple api requests from a list?
Is there a faster/more efficient way than a loop to make multiple api requests from a list?

Time:10-15

I have a list of about 70 items and I need to make an api request for each item. I am currently using a for loop to go through each item in that list and make the api request. Is there a more efficient way to do this? I would imagine there is a way to make the api requests simultaneously instead of looping.

CodePudding user response:

You should use some kind of parallelism if possible, such as the Pool class.

CodePudding user response:

I would use httpx

async def send_request(item):
    async with httpx.AsyncClient() as client:
       r = await client.get('https://www.example.com/', data=item)
    return r.json()

async def main():
    data = [{"hi": 1}, {"hi": 2}]
    coros = [send_request(x) for x in data]
    results = await asyncio.gather(*coros)
    return results


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

CodePudding user response:

you can use standard python library threading:

def name_func(param):
    do something

thread1=threading.Thread(target=name_func, args =(a,))
thread2=threading.Thread(target=name_func, args =(b,))

thread1.start()
thread2.start()
  • Related