mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-03 11:04:51 -04:00
Add concurrently_execute function
This commit is contained in:
parent
35bb465b86
commit
8d73cd502b
4 changed files with 81 additions and 74 deletions
|
@ -16,7 +16,8 @@
|
|||
|
||||
from twisted.internet import defer, reactor
|
||||
|
||||
from .logcontext import PreserveLoggingContext
|
||||
from .logcontext import PreserveLoggingContext, preserve_fn
|
||||
from synapse.util import unwrapFirstError
|
||||
|
||||
|
||||
@defer.inlineCallbacks
|
||||
|
@ -107,3 +108,32 @@ class ObservableDeferred(object):
|
|||
return "<ObservableDeferred object at %s, result=%r, _deferred=%r>" % (
|
||||
id(self), self._result, self._deferred,
|
||||
)
|
||||
|
||||
|
||||
def concurrently_execute(func, args, limit):
|
||||
"""Executes the function with each argument conncurrently while limiting
|
||||
the number of concurrent executions.
|
||||
|
||||
Args:
|
||||
func (func): Function to execute, should return a deferred.
|
||||
args (list): List of arguments to pass to func, each invocation of func
|
||||
gets a signle argument.
|
||||
limit (int): Maximum number of conccurent executions.
|
||||
|
||||
Returns:
|
||||
deferred
|
||||
"""
|
||||
it = iter(args)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def _concurrently_execute_inner():
|
||||
try:
|
||||
while True:
|
||||
yield func(it.next())
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
return defer.gatherResults([
|
||||
preserve_fn(_concurrently_execute_inner)()
|
||||
for _ in xrange(limit)
|
||||
], consumeErrors=True).addErrback(unwrapFirstError)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue