aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/_threads/_memory.py
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2024-11-12 07:54:50 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2024-11-12 08:05:59 +0300
commit55cec9f6b0618fb3570fc8ef66aad151f4932591 (patch)
tree9198c2ca0b0305269062c3674ce79f19c4990e65 /contrib/python/Twisted/py3/twisted/_threads/_memory.py
parentb77b1fbf262ea4f40e33a60ce32c4db4e5e49015 (diff)
downloadydb-55cec9f6b0618fb3570fc8ef66aad151f4932591.tar.gz
Intermediate changes
commit_hash:c229701a8b4f4d9ee57ce1ed763099d862d53fa6
Diffstat (limited to 'contrib/python/Twisted/py3/twisted/_threads/_memory.py')
-rw-r--r--contrib/python/Twisted/py3/twisted/_threads/_memory.py35
1 files changed, 24 insertions, 11 deletions
diff --git a/contrib/python/Twisted/py3/twisted/_threads/_memory.py b/contrib/python/Twisted/py3/twisted/_threads/_memory.py
index 4c56db02ae..5483b8e577 100644
--- a/contrib/python/Twisted/py3/twisted/_threads/_memory.py
+++ b/contrib/python/Twisted/py3/twisted/_threads/_memory.py
@@ -6,16 +6,25 @@
Implementation of an in-memory worker that defers execution.
"""
+from __future__ import annotations
+
+from enum import Enum, auto
+from typing import Callable, Literal
from zope.interface import implementer
-from . import IWorker
from ._convenience import Quit
+from ._ithreads import IExclusiveWorker
+
+
+class NoMore(Enum):
+ Work = auto()
+
-NoMoreWork = object()
+NoMoreWork = NoMore.Work
-@implementer(IWorker)
+@implementer(IExclusiveWorker)
class MemoryWorker:
"""
An L{IWorker} that queues work for later performance.
@@ -24,14 +33,17 @@ class MemoryWorker:
@type _quit: L{Quit}
"""
- def __init__(self, pending=list):
+ def __init__(
+ self,
+ pending: Callable[[], list[Callable[[], object] | Literal[NoMore.Work]]] = list,
+ ) -> None:
"""
Create a L{MemoryWorker}.
"""
self._quit = Quit()
self._pending = pending()
- def do(self, work):
+ def do(self, work: Callable[[], object]) -> None:
"""
Queue some work for to perform later; see L{createMemoryWorker}.
@@ -40,7 +52,7 @@ class MemoryWorker:
self._quit.check()
self._pending.append(work)
- def quit(self):
+ def quit(self) -> None:
"""
Quit this worker.
"""
@@ -48,22 +60,23 @@ class MemoryWorker:
self._pending.append(NoMoreWork)
-def createMemoryWorker():
+def createMemoryWorker() -> tuple[MemoryWorker, Callable[[], bool]]:
"""
Create an L{IWorker} that does nothing but defer work, to be performed
later.
@return: a worker that will enqueue work to perform later, and a callable
that will perform one element of that work.
- @rtype: 2-L{tuple} of (L{IWorker}, L{callable})
"""
- def perform():
+ def perform() -> bool:
if not worker._pending:
return False
- if worker._pending[0] is NoMoreWork:
+ peek = worker._pending[0]
+ if peek is NoMoreWork:
return False
- worker._pending.pop(0)()
+ worker._pending.pop(0)
+ peek()
return True
worker = MemoryWorker()