⏺ The remaining ~79 defer.* usages are all in code that depends on the Twisted reactor to schedule and resolve Deferreds. They'll be eliminated when the entry point switches from reactor.run() to asyncio.run(). At that point:

- defer.Deferred() → asyncio.Future() or asyncio.Event()
  - defer.gatherResults() → asyncio.gather()
  - defer.succeed/fail → direct returns / asyncio.Future.set_result/set_exception
  - defer.TimeoutError → asyncio.TimeoutError
  - defer.ensureDeferred → asyncio.ensure_future or direct await
  - Linearizer, ReadWriteLock, etc. → NativeLinearizer, NativeReadWriteLock
This commit is contained in:
Matthew Hodgson
2026-03-21 21:12:46 +00:00
parent 8e1c26067b
commit 56ffcebdcf
4 changed files with 18 additions and 6 deletions

View File

@@ -43,7 +43,7 @@ import unpaddedbase64
from prometheus_client import Counter
try:
from twisted.internet.defer import CancelledError
from asyncio import CancelledError
from twisted.web.server import Request
except ImportError:
pass

View File

@@ -36,7 +36,7 @@ except ImportError:
try:
from twisted.application.internet import ClientService
from twisted.internet.defer import CancelledError
from asyncio import CancelledError
from twisted.internet.defer import Deferred
from twisted.internet.endpoints import (
HostnameEndpoint,

View File

@@ -59,7 +59,7 @@ except ImportError:
pass
try:
from twisted.internet.defer import CancelledError
from asyncio import CancelledError
from twisted.internet.interfaces import (
IReactorCore,
IReactorPluggableNameResolver,

View File

@@ -1146,7 +1146,8 @@ class NativeLinearizer:
# do work
"""
def __init__(self, name: str, max_count: int = 1) -> None:
def __init__(self, name: str, max_count: int = 1, clock: Any = None) -> None:
# clock parameter accepted for backward compatibility with Linearizer(clock=...)
self.name = name
self.max_count = max_count
self._key_to_entry: dict[Hashable, _NativeLinearizerEntry] = {}
@@ -1350,7 +1351,8 @@ class NativeAwakenableSleeper:
Allows explicit waking of sleeping coroutines by name.
"""
def __init__(self) -> None:
def __init__(self, clock: Any = None) -> None:
# clock parameter accepted for backward compatibility
self._streams: dict[str, set[asyncio.Event]] = {}
def wake(self, name: str) -> None:
@@ -1383,7 +1385,8 @@ class NativeEvent:
Like threading.Event but for asyncio code.
"""
def __init__(self) -> None:
def __init__(self, clock: Any = None) -> None:
# clock parameter accepted for backward compatibility
self._event = asyncio.Event()
def set(self) -> None:
@@ -1490,3 +1493,12 @@ class DeferredEvent:
pass
return self.is_set()
# ===========================================================================
# The native asyncio versions (NativeLinearizer, NativeReadWriteLock,
# NativeAwakenableSleeper, NativeEvent) are available above and will replace
# the Deferred-based versions once the entry point switches from reactor.run()
# to asyncio.run(). Until then, the old classes remain as they need the
# Twisted reactor to drive their Deferred-based internals.
# ===========================================================================