@@ -212,12 +212,10 @@ def __init__(self, shard):
212212
213213 self ._executors = []
214214
215- # Set up and spawn the sweeper.
216- #
217- # TODO: link to greenlet and react to its death.
218215 self ._sweeper_start = None
219216 self ._sweeper_event = Event ()
220- gevent .spawn (self ._sweeper_loop )
217+ self ._sweeper_started = False
218+ self ._sweeper_timeout = None
221219
222220 def add_executor (self , executor ):
223221 """Add an executor for the service.
@@ -264,21 +262,27 @@ def dequeue(self, operation):
264262 for executor in self ._executors :
265263 executor .dequeue (operation )
266264
267- def _sweeper_timeout (self ):
268- """Return how frequently to run the sweeper loop .
265+ def start_sweeper (self , timeout ):
266+ """Start sweeper loop with given timeout .
269267
270- return (float|None): timeout in seconds, or None for no
271- sweeping.
268+ timeout (float): timeout in seconds.
272269
273270 """
274- return None
271+ if not self ._sweeper_started :
272+ self ._sweeper_started = True
273+ self ._sweeper_timeout = timeout
274+
275+ # TODO: link to greenlet and react to its death.
276+ gevent .spawn (self ._sweeper_loop )
277+ else :
278+ logger .warning ("Service tried to start the sweeper loop twice." )
275279
276280 def _sweeper_loop (self ):
277281 """Regularly check for missed operations.
278282
279- Run the sweep once every _sweeper_timeout() seconds but make
283+ Run the sweep once every _sweeper_timeout seconds but make
280284 sure that no two sweeps run simultaneously. That is, start a
281- new sweep _sweeper_timeout() seconds after the previous one
285+ new sweep _sweeper_timeout seconds after the previous one
282286 started or when the previous one finished, whatever comes
283287 last.
284288
@@ -291,10 +295,6 @@ def _sweeper_loop(self):
291295 suppressed, because the loop must go on.
292296
293297 """
294- # If the timeout is None, it means the subclass does not want
295- # a sweeper.
296- if self ._sweeper_timeout () is None :
297- return
298298 while True :
299299 self ._sweeper_start = monotonic_time ()
300300 self ._sweeper_event .clear ()
@@ -306,7 +306,7 @@ def _sweeper_loop(self):
306306 "operations." , exc_info = True )
307307
308308 self ._sweeper_event .wait (max (self ._sweeper_start +
309- self ._sweeper_timeout () -
309+ self ._sweeper_timeout -
310310 monotonic_time (), 0 ))
311311
312312 def _sweep (self ):
0 commit comments