def call_singleton(self, function, identifier, *args, **kwargs):
"""
Executes *function* if no other function with the given *identifier*
is already running. If a function is currently running with the given
*identifier* the passed *function* will be called when the first
function is complete.
In other words, functions called via this method will be executed in
sequence with each function being called after the first is complete.
The function will be passed any given *args* and *kwargs* just like
:meth:`AsyncRunner.call`.
If 'callback' is passed as a keyword argument (*kwargs*) it will be
called with the result when complete.
"""
callback = kwargs.pop('callback', None)
if identifier in ONE_CALLS:
ONE_CALLS[identifier]['queue'].append(
(function, args, kwargs, callback))
else:
from collections import deque
future = self.executor.submit(safe_call, function, *args, **kwargs)
ONE_CALLS[identifier] = {
'future': future,
'queue': deque()
}
if callback:
done_callback(
ONE_CALLS[identifier]['future'],
lambda f: callback(f.result()))
completed = partial(_call_complete, self, identifier)
done_callback(ONE_CALLS[identifier]['future'], completed)
#print 'ONE_CALLS',ONE_CALLS
#print 'identifier',identifier
return ONE_CALLS[identifier]['future']
评论列表
文章目录