8

当我在 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
4

4 回答 4

4

我有完全相同的错误。我打开了这个问题

如果您想继续使用当前的 Python 版本,我找到了临时解决方案,即添加以下行(如issueasgiref\__init__.py中所建议的那样):

if sys.platform == "win32" and sys.version_info >= (3, 8, 0):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
于 2020-02-24T08:29:07.123 回答
2

不仅 Django,你也会在使用 Flask 时遇到这个错误,因为 Flask 2.0.0 使用 asgiref 来支持async.

这个 bug 在 Python 3.9 中已经修复,所以有两种解决方案:

  • 升级到 Python 3.9
  • 将临时修复如下(基于Dennis B 的回答)添加到您的条目脚本,例如 Flaskapp.py或 Django 的manager.py
# top of the file
import sys, asyncio

if sys.platform == "win32" and (3, 8, 0) <= sys.version_info < (3, 9, 0):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
于 2021-04-28T22:53:12.387 回答
1

升级到 python 3.8.2 ,在我的 Windows 服务器上运行良好。

发行说明 https://docs.python.org/release/3.8.2/whatsnew/changelog.html#python-3-8-2-final

问题 bpo-34679:修复了 asynci.ProactorEventLoop.close() 现在仅在主线程中调用 signal.set_wakeup_fd()。

于 2020-02-27T20:53:01.713 回答
1

我不得不降级到 Python 3.7,然后它开始完美运行。

于 2020-06-26T03:53:57.003 回答