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

Commit 2ce27a2

Browse files
authored
Add experimental environment variable to enable asyncio reactor (#12135)
1 parent ca9234a commit 2ce27a2

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

changelog.d/12135.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add experimental env var `SYNAPSE_ASYNC_IO_REACTOR` that causes Synapse to use the asyncio reactor for Twisted.

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,6 @@ ignore_missing_imports = True
353353

354354
[mypy-zope]
355355
ignore_missing_imports = True
356+
357+
[mypy-incremental.*]
358+
ignore_missing_imports = True

synapse/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@
2525
print("Synapse requires Python 3.7 or above.")
2626
sys.exit(1)
2727

28+
# Allow using the asyncio reactor via env var.
29+
if bool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", False)):
30+
try:
31+
from incremental import Version
32+
33+
import twisted
34+
35+
# We need a bugfix that is included in Twisted 21.2.0:
36+
# https://twistedmatrix.com/trac/ticket/9787
37+
if twisted.version < Version("Twisted", 21, 2, 0):
38+
print("Using asyncio reactor requires Twisted>=21.2.0")
39+
sys.exit(1)
40+
41+
import asyncio
42+
43+
from twisted.internet import asyncioreactor
44+
45+
asyncioreactor.install(asyncio.get_event_loop())
46+
except ImportError:
47+
pass
48+
2849
# Twisted and canonicaljson will fail to import when this file is executed to
2950
# get the __version__ during a fresh install. That's OK and subsequent calls to
3051
# actually start Synapse will import these libraries fine.

0 commit comments

Comments
 (0)