python-trio / trio

Trio – a friendly Python library for async concurrency and I/O
https://trio.readthedocs.io
Other
5.98k stars 325 forks source link

No support aiohttp? #3012

Closed allrobot closed 3 weeks ago

allrobot commented 3 weeks ago
async def fetch_detail(url: str, aiohttp_session: aiohttp.ClientSession):
    async with aiohttp_session.get(url) as response:
        html = await response.text()
        return html

async def aiomultiprocess_main():
    main_url = "http://am.adianshi.com:6805"
    async with aiohttp.ClientSession() as aiohttp_session:
        async with trio.open_nursery() as nursery:
            nursery.start_soon(fetch_detail, main_url,aiohttp_session)

if __name__ == "__main__":
    trio.run(aiomultiprocess_main)

It report error:

C:\ProgramData\anaconda3\envs\python311\python.exe C:\Users\Administrator\Personal_scripts\pythonProject\temp.py 
C:\Users\Administrator\Personal_scripts\pythonProject\temp.py:50: DeprecationWarning: The object should be created within an async function
  async with aiohttp.ClientSession() as aiohttp_session:
Traceback (most recent call last):
  File "C:\Users\Administrator\Personal_scripts\pythonProject\temp.py", line 51, in aiomultiprocess_main
    async with trio.open_nursery() as nursery:
  File "C:\ProgramData\anaconda3\envs\python311\Lib\site-packages\trio\_core\_run.py", line 813, in __aexit__
    raise combined_error_from_nursery
  File "C:\Users\Administrator\Personal_scripts\pythonProject\temp.py", line 38, in fetch_detail
    async with aiohttp_session.get(url) as response:
  File "C:\ProgramData\anaconda3\envs\python311\Lib\site-packages\aiohttp\client.py", line 1197, in __aenter__
    self._resp = await self._coro
                 ^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\envs\python311\Lib\site-packages\aiohttp\client.py", line 507, in _request
    with timer:
  File "C:\ProgramData\anaconda3\envs\python311\Lib\site-packages\aiohttp\helpers.py", line 715, in __enter__
    raise RuntimeError(
RuntimeError: Timeout context manager should be used inside a task

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Administrator\Personal_scripts\pythonProject\temp.py", line 56, in <module>
    trio.run(aiomultiprocess_main)
  File "C:\ProgramData\anaconda3\envs\python311\Lib\site-packages\trio\_core\_run.py", line 1946, in run
    raise runner.main_task_outcome.error
  File "C:\Users\Administrator\Personal_scripts\pythonProject\temp.py", line 50, in aiomultiprocess_main
    async with aiohttp.ClientSession() as aiohttp_session:
TypeError: trio.run received unrecognized yield message None. Are you trying to use a library written for some other framework like asyncio? That won't work without some kind of compatibility shim.

I must use asyncio code:

async def main():
    main_url = "http://am.adianshi.com:6805"
    async with aiohttp.ClientSession() as aiohttp_session:
        html=await asyncio.create_task(fetch_detail(main_url, aiohttp_session))
        print(html)
        print(download_dict)

if __name__ == "__main__":
    asyncio.run(main())

It can work.

A5rocks commented 3 weeks ago

Unfortunately (or fortunately?) trio doesn't natively support asyncio libraries. You can use something like trio-asyncio to run asyncio libraries in trio or maybe try an http client that supports trio like httpx.

jakkdl commented 3 weeks ago

+1 for https://www.python-httpx.org/ instead of aiohttp. Their feature sets largely overlap.

See https://trio.readthedocs.io/en/stable/awesome-trio-libraries.html for more suggestions on trio/anyio-compatible libraries