Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclosed client session #4342

Closed
viponedream opened this issue Nov 14, 2019 · 2 comments
Closed

Unclosed client session #4342

viponedream opened this issue Nov 14, 2019 · 2 comments

Comments

@viponedream
Copy link

viponedream commented Nov 14, 2019

Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x00000153A95E9E10>

it shows the error above.

================================
here is my code:

class Test:

# use aiohttp
async def aio_get(self, session=None, url='', params={}, headers={}, proxy='',  timeout=10, verify_ssl=False):
    try:
        async with session.get(url=url, params=params, headers=headers, proxy=proxy, verify_ssl=verify_ssl) as resp:
            r = await resp.read()
            return r
    except:
        return None
# main 
async def async_verify(self, work_id:int=1,  work_count:int=100000):
     for x in range(work_count):
         session = aiohttp.ClientSession()
         content = await self.aio_get(session=session, url=home_url, params={}, headers=headers, proxy=proxies, verify_ssl=False)
        if content is None:   
             continue
       # do something
       content = await self.aio_get(session=session, url=home_url, params={}, headers=headers, proxy=proxies, verify_ssl=False)
      if content is None:   
           continue
      # do something
      # do something
      # do something
       session.close()

# run loop 
def start(self):
    loop = asyncio.get_event_loop()
    tasks = []
    for i in range(10):
        task = asyncio.ensure_future(self.async_verify(work_id=i, work_count=work_count))
        tasks.append(task)

    loop.run_until_complete(asyncio.wait(tasks))          
@AtomsForPeace
Copy link
Contributor

If content is None:   
    continue

When you continue you will never come to session.close()

Could that be your problem?

@AtomsForPeace
Copy link
Contributor

Have a look at this: #4257

I'll close this here but you can ask your question over on discourse in case my answer doesn't solve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants