0

I'm trying write a python script that makes a bunch of parallel http requests. Here is what I have so far.

async def deleteLinkRecords(req_headers):
  async with aiohttp.ClientSession(trust_env=True) as session:
   idm_url = "https://example.com"

   tasks = []
   for id in linkIdArr:
     del_url = idm_url + id
     tasks.append(make_request(session, del_url, req_headers))

  results = await asyncio.gather(*tasks)
  print(results)

async def make_request(session, url, req_headers):
    async with session.delete(url = url, headers = req_headers) as resp:
     return await resp.json()

my_headers = {
   "Authorization": "Bearer XXX"
}

linkIdArr = ["abc", "def"....]

asyncio.run(deleteLinkRecords(req_headers = my_headers))

When I run this script, I am getting the following error.

Traceback (most recent call last):
  File "C:\Users\mycode.py", line 48, in <module>
    asyncio.run(deleteLinkRecords(req_headers = my_headers))
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\mycode.py", line 20, in deleteLinkRecords
    results = await asyncio.gather(*tasks)
  File "C:\Users\mycode.py", line 28, in make_requests
    async with session.delete(url = url, headers = req_headers) as resp:
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 1197, in __aenter__
    self._resp = await self._coro
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 428, in _request
    raise RuntimeError("Session is closed")
RuntimeError: Session is closed

How can I fix this issue?

2 Answers 2

1

If you want to reuse the ClientSession then you must add the asyncio.gather inside the same context manager.

async def deleteLinkRecords(req_headers):
  async with aiohttp.ClientSession(trust_env=True) as session:
   idm_url = "https://example.com"

   tasks = []
   for id in linkIdArr:
     del_url = idm_url + id
     tasks.append(make_request(session, del_url, req_headers))

   results = await asyncio.gather(*tasks)
   print(results)

async def make_request(session, url, req_headers):
    async with session.delete(url = url, headers = req_headers) as resp:
     return await resp.json()

my_headers = {
   "Authorization": "Bearer XXX"
}

linkIdArr = ["abc", "def"....]

asyncio.run(deleteLinkRecords(req_headers = my_headers))

Sign up to request clarification or add additional context in comments.

Comments

1

Check out this post. Because you are using with to handle your sessions, it closes as soon as it is done. So when you try to use it later it has already been closed.

So for example, instead of

async with aiohttp.ClientSession(trust_env=True) as session:

and

async with session.delete(url = url, headers = req_headers) as resp:

just store the session in a variable like this:

session = aiohttp.ClientSession(trust_env=True)
resp = session.delete(url = url, headers = req_headers)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.