当我在 Windows 10 专业版上使用 Apache2.4.41 + Python 3.8.1 + Django 3.0.2 + MySQL 8.0.19 运行我的 Django Web 应用程序时,它会在 / 处引发值错误。set_wakeup_fd 仅适用于主线程。
此问题是 Python 3.8 中回归的结果,并于 11 月在 Python 的后续版本中得到修复。有关更多详细信息 - https://bugs.python.org/issue38563。
错误的堆栈跟踪如下 -
Environment:
Request Method: GET
Request URL: http://127.0.0.1/
Django Version: 3.0.2
Python Version: 3.8.1
Installed Applications:
['Analysis.apps.AnalysisConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "c:\python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "c:\python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "c:\python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "F:\IndianMarketAnalysis\ISMA\Analysis\views.py", line 8, in home
date_list = dict(d.date_list())
File "F:\IndianMarketAnalysis\ISMA\Analysis\models.py", line 7, in date_list
with connection.cursor() as cursor:
File "c:\python38\lib\site-packages\django\utils\asyncio.py", line 19, in inner
event_loop = asyncio.get_event_loop()
File "c:\python38\lib\asyncio\events.py", line 636, in get_event_loop
self.set_event_loop(self.new_event_loop())
File "c:\python38\lib\asyncio\events.py", line 656, in new_event_loop
return self._loop_factory()
File "c:\python38\lib\asyncio\windows_events.py", line 310, in __init__
super().__init__(proactor)
File "c:\python38\lib\asyncio\proactor_events.py", line 632, in __init__
signal.set_wakeup_fd(self._csock.fileno())
Exception Type: ValueError at /
Exception Value: set_wakeup_fd only works in main thread
此外,此应用程序在本地开发环境中运行良好,使用 - python manage.py runserver。
请建议下一步的步骤。
同样的问题发生在 Flask 2.0.0 ( pip install --pre flask
) 上:
@app.get('/')
async def say_hello():
return {'message': 'Hello!'}
> flask run
Traceback (most recent call last):
File "c:\users\greyli\...\venv\lib\site-packages\flask\app.py", line 1953, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\greyli\...\venv\lib\site-packages\flask\app.py", line 1454, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\users\greyli\...\venv\lib\site-packages\flask\app.py", line 1452, in full_dispatch_request
rv = self.dispatch_request()
File "c:\users\greyli\...\app.py", line 318, in dispatch_request
return self.view_functions[rule.endpoint](*req.view_args.values())
File "c:\users\greyli\...\venv\lib\site-packages\flask\helpers.py", line 781, in outer
return async_to_sync(inner)(*args, **kwargs)
File "c:\users\...\venv\lib\site-packages\asgiref\sync.py", line 203, in __call__
loop_future.result()
File "C:\Users\greyli\.pyenv\pyenv-win\versions\3.8.0\lib\concurrent\futures\_base.py", line 432, in result
return self.__get_result()
File "C:\Users\greyli\.pyenv\pyenv-win\versions\3.8.0\lib\concurrent\futures\_base.py", line 388, in __get_result
raise self._exception
File "C:\Users\greyli\.pyenv\pyenv-win\versions\3.8.0\lib\concurrent\futures\thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "c:\users\greyli\...\venv\lib\site-packages\asgiref\sync.py", line 256, in _run_event_loop
loop.close()
File "C:\Users\greyli\.pyenv\pyenv-win\versions\3.8.0\lib\asyncio\proactor_events.py", line 679, in
close
signal.set_wakeup_fd(-1)
ValueError: set_wakeup_fd only works in main thread