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

pytinstrument does not track the call stack of __aenter__ and __aexit__ #282

Open
Whisht opened this issue Nov 22, 2023 · 0 comments
Open

Comments

@Whisht
Copy link

Whisht commented Nov 22, 2023

I am using pyinstrument to track an async code with aiohttp. My purpose is that to analyze why my custom __aexit__() is not called, but there are no record about my custom code whether __aenter__() or __aexit__().

Here is my code named test_aio.py, this is the correct version where I manually call __aenter__ and __aexit__.

import asyncio, aiohttp

class AioHttpSession:
    def __init__(self) -> None:
        self._session = None

    async def __aenter__(self):
        if self._session is None:
            self._session = await aiohttp.ClientSession().__aenter__()
        return self

    async def __aexit__(self):
        if self._session is None:
            raise RuntimeError("session it not initilaized")
        await self._session.__aexit__(None, None, None)

    async def request(self, url="http://www.baidu.com"):
        if self._session is None:
            await self.__aenter__()
        async with self._session.get(url) as response:
            data = await response.text()
            print(self._session)
            return data

class MyAPP:
    def __init__(self) -> None:
        self.client = AioHttpSession()

    async def fetch(self):
        session = await self.client.__aenter__()
        try:
            resp = await session.request()
            return resp
        except Exception as e:
            if resp is not None:
                await resp.release()
            await session.__aexit__(None, None, None)
            raise

async def main():
    app = MyAPP()
    tasks = [app.fetch() for _ in range(3)]
    res = await asyncio.gather(*tasks)
    await app.client.__aexit__()

asyncio.run(main())

And I run pyinstrument with pyinstrument --show-all test_aio.py. But no called information about AioHttpSession.__aenter__() or `aexit().

@Whisht Whisht changed the title pytinstrument not track the call stack of __aenter__ and __aexit__ pytinstrument does not track the call stack of __aenter__ and __aexit__ Nov 22, 2023
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

1 participant