python-3.x - 线程中的服务器(Python3.9.0+aiohttp):RuntimeError: can't register atexit after shutdown
问题描述
这段代码(在线程中运行的最小服务器,代码取自此处)在 Python3.8.3 中运行良好,但在 Python3.9.0 中引发错误消息:
import asyncio
import threading
from aiohttp import web
def aiohttp_server():
def say_hello(request):
return web.Response(text='Hello, world')
app = web.Application()
app.add_routes([web.get('/', say_hello)])
runner = web.AppRunner(app)
return runner
def run_server(runner):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(runner.setup())
site = web.TCPSite(runner, 'localhost', 8080)
loop.run_until_complete(site.start())
loop.run_forever()
t = threading.Thread(target=run_server, args=(aiohttp_server(),))
t.start()
错误信息:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
self.run()
File "/usr/lib/python3.9/threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "/home/alkhinoos/nikw/nikw/z2.py", line 21, in run_server
loop.run_until_complete(site.start())
File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/usr/lib/python3.9/site-packages/aiohttp/web_runner.py", line 121, in start
self._server = await loop.create_server(
File "/usr/lib/python3.9/asyncio/base_events.py", line 1460, in create_server
infos = await tasks.gather(*fs, loop=self)
File "/usr/lib/python3.9/asyncio/base_events.py", line 1400, in _create_server_getaddrinfo
infos = await self._ensure_resolved((host, port), family=family,
File "/usr/lib/python3.9/asyncio/base_events.py", line 1396, in _ensure_resolved
return await loop.getaddrinfo(host, port, family=family, type=type,
File "/usr/lib/python3.9/asyncio/base_events.py", line 856, in getaddrinfo
return await self.run_in_executor(
File "/usr/lib/python3.9/asyncio/base_events.py", line 809, in run_in_executor
executor = concurrent.futures.ThreadPoolExecutor(
File "/usr/lib/python3.9/concurrent/futures/__init__.py", line 49, in __getattr__
from .thread import ThreadPoolExecutor as te
File "/usr/lib/python3.9/concurrent/futures/thread.py", line 37, in <module>
threading._register_atexit(_python_exit)
File "/usr/lib/python3.9/threading.py", line 1374, in _register_atexit
raise RuntimeError("can't register atexit after shutdown")
RuntimeError: can't register atexit after shutdown
这是怎么回事 ?Python 3.9.1 也出现了同样的问题。这个问题用 Python 3.9.2 解决了吗?也许这里是一个相对的问题。
解决方案
如 Python 手册中所述 -线程对象
其他线程可以调用线程的 join() 方法。这会阻塞调用线程,直到调用 join() 方法的线程终止。
调用t.start()
主线程后,主线程将退出。然后该过程结束。
如果你想永远运行子线程或者直到它退出,你应该t.join()
在t.start()
.
推荐阅读
- css - 使用外部 CSS 的 Blogger 条件标记
- python - 如何在 Django admin 中测试覆盖功能?
- javascript - 反应:仅当条件为真时才渲染孩子
- swift - View on View 控制器在 Xcode 11 上都是方形的,怎么样?
- qliksense - 如何在 Qlik Sense 中默认显示最近一周的交易量
- javascript - 如何在 javscript/node 中导入对象
- python - Python 如何处理子包?
- vba - 选择单元格时创建文件夹和子文件夹
- vue.js - 你如何在 firebase 服务工作者和 Vue/Vuex 网络应用程序之间进行通信
- xamarin - 如何在 Xamarin.iOS 中使用 Google 的人员 API?