aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/_threads/_memory.py
diff options
context:
space:
mode:
authorshmel1k <shmel1k@ydb.tech>2023-11-26 18:16:14 +0300
committershmel1k <shmel1k@ydb.tech>2023-11-26 18:43:30 +0300
commitb8cf9e88f4c5c64d9406af533d8948deb050d695 (patch)
tree218eb61fb3c3b96ec08b4d8cdfef383104a87d63 /contrib/python/Twisted/py3/twisted/_threads/_memory.py
parent523f645a83a0ec97a0332dbc3863bb354c92a328 (diff)
downloadydb-b8cf9e88f4c5c64d9406af533d8948deb050d695.tar.gz
add kikimr_configure
Diffstat (limited to 'contrib/python/Twisted/py3/twisted/_threads/_memory.py')
-rw-r--r--contrib/python/Twisted/py3/twisted/_threads/_memory.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/contrib/python/Twisted/py3/twisted/_threads/_memory.py b/contrib/python/Twisted/py3/twisted/_threads/_memory.py
new file mode 100644
index 0000000000..4c56db02ae
--- /dev/null
+++ b/contrib/python/Twisted/py3/twisted/_threads/_memory.py
@@ -0,0 +1,70 @@
+# -*- test-case-name: twisted._threads.test.test_memory -*-
+# Copyright (c) Twisted Matrix Laboratories.
+# See LICENSE for details.
+
+"""
+Implementation of an in-memory worker that defers execution.
+"""
+
+
+from zope.interface import implementer
+
+from . import IWorker
+from ._convenience import Quit
+
+NoMoreWork = object()
+
+
+@implementer(IWorker)
+class MemoryWorker:
+ """
+ An L{IWorker} that queues work for later performance.
+
+ @ivar _quit: a flag indicating
+ @type _quit: L{Quit}
+ """
+
+ def __init__(self, pending=list):
+ """
+ Create a L{MemoryWorker}.
+ """
+ self._quit = Quit()
+ self._pending = pending()
+
+ def do(self, work):
+ """
+ Queue some work for to perform later; see L{createMemoryWorker}.
+
+ @param work: The work to perform.
+ """
+ self._quit.check()
+ self._pending.append(work)
+
+ def quit(self):
+ """
+ Quit this worker.
+ """
+ self._quit.set()
+ self._pending.append(NoMoreWork)
+
+
+def createMemoryWorker():
+ """
+ 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():
+ if not worker._pending:
+ return False
+ if worker._pending[0] is NoMoreWork:
+ return False
+ worker._pending.pop(0)()
+ return True
+
+ worker = MemoryWorker()
+ return (worker, perform)