Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add experimental environment variable to enable asyncio reactor #12135

Merged
merged 9 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/12135.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add experimental env var `SYNAPSE_ASYNC_IO_REACTOR` that causes Synapse to use the asyncio reactor for Twisted.
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,6 @@ ignore_missing_imports = True

[mypy-zope]
ignore_missing_imports = True

[mypy-incremental.*]
ignore_missing_imports = True
21 changes: 21 additions & 0 deletions synapse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@
print("Synapse requires Python 3.7 or above.")
sys.exit(1)

# Allow using the asyncio reactor via env var.
if bool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", False)):
try:
from incremental import Version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should incremental be added into python_dependencies since we're now using it directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, though I was imagining this only being a temporary thing while we had the experimental flag, so not much point adding it to the dependencies?


import twisted

# We need a bugfix that is included in Twisted 21.2.0:
# https://twistedmatrix.com/trac/ticket/9787
if twisted.version < Version("Twisted", 21, 2, 0):
print("Using asyncio reactor requires Twisted>=21.2.0")
sys.exit(1)

import asyncio

from twisted.internet import asyncioreactor

asyncioreactor.install(asyncio.get_event_loop())
except ImportError:
pass
clokep marked this conversation as resolved.
Show resolved Hide resolved

# Twisted and canonicaljson will fail to import when this file is executed to
# get the __version__ during a fresh install. That's OK and subsequent calls to
# actually start Synapse will import these libraries fine.
Expand Down