Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

python - RuntimeError: Exception ignored in: <function _ProactorBasePipeTransport

I am using aiohttp, asyncio and codetiming to make concurrent requests. I've recently upgraded Python to version 3.9.0 and I'm getting a RuntimeError: Event loop is closed after the program has runs. I am tring to make asynchronous requests using queue data structure...

import asyncio
import aiohttp
from codetiming import Timer
async def task(name, work_queue):
    timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
    async with aiohttp.ClientSession() as session:
        while not work_queue.empty():
            url = await work_queue.get()
            print(f"Task {name} getting URL: {url}")
            timer.start()
            async with session.get(url) as response:
                await response.text()
            timer.stop()
async def main():
    # Create the queue of work
    work_queue = asyncio.Queue()
    # Put some work in the queue
    for url in [
        "http://google.com",
        "http://yahoo.com",
        "http://linkedin.com",
        "http://apple.com",
        "http://microsoft.com",
        "http://facebook.com",
        "http://twitter.com",
    ]:
        await work_queue.put(url)

    # Run the tasks
    with Timer(text="
Total elapsed time: {:.1f}"):
        await asyncio.gather(
            asyncio.create_task(task("One", work_queue)),
            asyncio.create_task(task("Two", work_queue)),
        )

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

I also get my valid result but I don't know why I get this RunTime Error. The output is follows:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000020634355E50>
Traceback (most recent call last):
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 746, in call_soon
    self._check_closed()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000020634355E50>
Traceback (most recent call last):
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 746, in call_soon
    self._check_closed()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000020634355E50>
Traceback (most recent call last):
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 746, in call_soon
    self._check_closed()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000020634355E50>
Traceback (most recent call last):
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 746, in call_soon
    self._check_closed()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000020634355E50>
Traceback (most recent call last):
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 746, in call_soon
    self._check_closed()
  File "C:UsersDELLAppDataLocalProgramsPythonPython39libasyncioase_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...