[Unresolved problem log] python asyncio + aiohttp appears Exception ignored: RuntimeError ( 'Event loop is closed')

Looking for a long time could not find a reason to record it

Operating System: windows 8.1

python version: python 3.8.1

aiohttp Version: 3.6.2

Source:

 1 import asyncio
 2 import aiohttp
 3 
 4 
 5 async def aiohttp_request_test(url):
 6     async with aiohttp.request('get', url=url) as resp:
 7         await resp.text()
 8         # await asyncio.sleep(1) # A1
 9 
10 
11 async def main():
12     await asyncio.gather(aiohttp_request_test('https://www.baidu.com'))
13 
14 
15 asyncio.run(main())
16 # loop = asyncio.get_event_loop()
17 # loop.run_until_complete(main())

When running the above code, the following abnormalities occur:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x02A8C8E0>
Traceback (most recent call last):
  File "D:\Python\Anaconda3\envs\python38\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "D:\Python\Anaconda3\envs\python38\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "D:\Python\Anaconda3\envs\python38\lib\asyncio\base_events.py", line 715, in call_soon
    self._check_closed()
  File "D:\Python\Anaconda3\envs\python38\lib\asyncio\base_events.py", line 508, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

But with a await asyncio.sleep at the eighth row (1), no abnormality ( Scheme A )

Or

asyncio.run(main())

Read:

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

No exception. ( Scheme B )

(The actual use of Plan B will also appear when the abnormal, but failed to reproduce here, and the program can also be used to solve A)

I wonder why it is added, await asyncio.sleep (1) is not being given a

Also, if request an exception is thrown and caught, then (the aiohttp_request_test read as follows):

async def aiohttp_request_test(url):
    try:
        async with aiohttp.request('get', url=url) as resp:
            await resp.text()
    except:
        pass

It will not be reported RuntimeError: Event loop is closed

 

Sleepy, find a reason to come back to add.

Guess you like

Origin www.cnblogs.com/qyxfzmbz/p/12310524.html