Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32103 closed Bug (invalid)

NotImplementedError in twisted (with Python 3.8 on Windows)

Reported by: Andrey Zelenchuk Owned by: nobody
Component: Core (Management commands) Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The runserver management command does not work with Python ≥ 3.8 on Windows.

Steps to reproduce

  1. Open any Django project.
  2. Run python manage.py runserver.

Actual result

C:\Programs\Python38\python.exe C:/project/manage.py runserver
2020-10-11 18:12:08,823 INFO  [django.utils.autoreload] Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Programs\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Programs\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Programs\Python38\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Programs\Python38\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "C:\Programs\Python38\lib\site-packages\django\utils\autoreload.py", line 76, in raise_last_exception
    raise _exception[1]
  File "C:\Programs\Python38\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Programs\Python38\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Programs\Python38\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Programs\Python38\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Programs\Python38\lib\site-packages\django\apps\config.py", line 116, in create
    mod = import_module(mod_path)
  File "C:\Programs\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Programs\Python38\lib\site-packages\channels\apps.py", line 6, in <module>
    import daphne.server
  File "C:\Programs\Python38\lib\site-packages\daphne\server.py", line 20, in <module>
    asyncioreactor.install(twisted_loop)
  File "C:\Programs\Python38\lib\site-packages\twisted\internet\asyncioreactor.py", line 320, in install
    reactor = AsyncioSelectorReactor(eventloop)
  File "C:\Programs\Python38\lib\site-packages\twisted\internet\asyncioreactor.py", line 69, in __init__
    super().__init__()
  File "C:\Programs\Python38\lib\site-packages\twisted\internet\base.py", line 571, in __init__
    self.installWaker()
  File "C:\Programs\Python38\lib\site-packages\twisted\internet\posixbase.py", line 286, in installWaker
    self.addReader(self.waker)
  File "C:\Programs\Python38\lib\site-packages\twisted\internet\asyncioreactor.py", line 151, in addReader
    self._asyncioEventloop.add_reader(fd, callWithLogger, reader,
  File "C:\Programs\Python38\lib\asyncio\events.py", line 501, in add_reader
    raise NotImplementedError
NotImplementedError

Expected result

No errors.

Workaround

Add the following code to the manage.py file:

if sys.platform == 'win32' and sys.version_info >= (3, 8):
    import asyncio
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

References

Change History (6)

comment:1 by Mariusz Felisiak, 4 years ago

Resolution: invalid
Severity: Release blockerNormal
Status: newclosed
Summary: runserver: NotImplementedError (with Python 3.8 on Windows)NotImplementedError in twisted (with Python 3.8 on Windows)

This is not an issue in Django but in twisted. Please report it in their bug tracker.

comment:2 by Carlton Gibson, 4 years ago

Windows on Python 3.8+ uses ProactorEventLoop, which is not compatible with
Twisted. Does not implement add_writer/add_reader.
See https://bugs.python.org/issue37373
and https://twistedmatrix.com/trac/ticket/9766

Recent versions of Daphne automatically set the correct event loop policy in these cases.

comment:3 by Andrey Zelenchuk, 4 years ago

Windows on Python 3.8+ uses ProactorEventLoop

This is not accurate. Python 3.8+ on Windows uses the ProactorEventLoop by default. Python 3.8+ on Windows allows to use the SelectorEventLoop as well.

<...> which is not compatible with Twisted

So why then Django tries to use the ProactorEventLoop with Twisted?

Recent versions of Daphne automatically set the correct event loop policy in these cases.

Django's runserver management command does not use Daphne, does it? Why Django cannot set the correct event loop policy too?

This ticket is not invalid until the Django's documentation states that Django does not support Python 3.8+ on Windows. Besides, it looks like it can be fixed quite easily. So, could you reopen it, please?

comment:4 by Carlton Gibson, 4 years ago

Your stack trace says you're using channels, which does use Daphne/twisted yes.

comment:5 by Mariusz Felisiak, 4 years ago

This ticket is not invalid until the Django's documentation states that Django does not support Python 3.8+ on Windows. Besides, it looks like it can be fixed quite easily. So, could you reopen it, please?

Django supports Python 3.8 on Windows, bugs in other packages that you can use with Django doesn't change this, we cannot add workarounds for bugs which are not in Django itself. Also (as mentioned by Carlton) it's already fixed in a pre-release version of twisted.

in reply to:  4 comment:6 by Andrey Zelenchuk, 4 years ago

Your stack trace says you're using channels, which does use Daphne/twisted yes.

Yes, that's right, thank you! Sorry, I missed that Channels replaces the Django's runserver management command. This bug is invalid for Django, and it is already fixed in the recent version of Channels/Daphne/Twisted.

Note: See TracTickets for help on using tickets.
Back to Top