Python 3.x 线程中的服务器(Python3.9.0+;aiohttp):运行时错误:can';t在关机后注册atexit
这段代码(线程中运行的最小服务器,代码取自)在Python3.8.3中运行良好,但在Python3.9.0中会出现错误消息:Python 3.x 线程中的服务器(Python3.9.0+;aiohttp):运行时错误:can';t在关机后注册atexit,python-3.x,multithreading,aiohttp,Python 3.x,Multithreading,Aiohttp,这段代码(线程中运行的最小服务器,代码取自)在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()
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
线程1中的异常:
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3.9/threading.py”,第954行,在内部引导中
self.run()
文件“/usr/lib/python3.9/threading.py”,第892行,运行中
自我目标(*自我参数,**自我参数)
文件“/home/alkhinoos/nikw/nikw/z2.py”,第21行,在run_服务器中
循环。运行直到完成(site.start())
文件“/usr/lib/python3.9/asyncio/base\u events.py”,第642行,运行直到完成
返回future.result()
文件“/usr/lib/python3.9/site packages/aiohttp/web_runner.py”,第121行,开始
self.\u server=wait loop.create\u server(
文件“/usr/lib/python3.9/asyncio/base_events.py”,第1460行,位于create_server中
infos=等待任务。聚集(*fs,loop=self)
文件“/usr/lib/python3.9/asyncio/base\u events.py”,第1400行,位于\u create\u server\u getaddrinfo中
infos=等待自我。\确保\u已解决((主机、端口),family=family,
文件“/usr/lib/python3.9/asyncio/base\u events.py”,第1396行,在
return wait loop.getaddrinfo(主机、端口、系列=系列、类型=类型、,
文件“/usr/lib/python3.9/asyncio/base_events.py”,第856行,位于getaddrinfo中
返回等待自我运行执行器(
文件“/usr/lib/python3.9/asyncio/base_events.py”,第809行,在run_in_executor中
executor=concurrent.futures.ThreadPoolExecutor(
文件“/usr/lib/python3.9/concurrent/futures/_init__.py”,第49行,在_getattr中__
from.thread将ThreadPoolExecutor作为te导入
文件“/usr/lib/python3.9/concurrent/futures/thread.py”,第37行,在
线程。_寄存器_atexit(_python_exit)
文件“/usr/lib/python3.9/threading.py”,第1374行,在寄存器atexit中
raise RUNTIMERROR(“关闭后无法注册atexit”)
运行时错误:关闭后无法注册atexit
发生了什么事?Python 3.9.1也出现了同样的问题。这个问题用Python 3.9.2解决了吗?也许。不确定你做了什么,但我用了
127.0.0.1
而不是localhost
,错误得到了解决!如果你想让它永远运行,你应该使用t.join()
在t.start()之后等待线程结束。