Package buildbot :: Package util :: Module eventual
[frames] | no frames]

Source Code for Module buildbot.util.eventual

 1  # This file is part of Buildbot.  Buildbot is free software: you can 
 2  # redistribute it and/or modify it under the terms of the GNU General Public 
 3  # License as published by the Free Software Foundation, version 2. 
 4  # 
 5  # This program is distributed in the hope that it will be useful, but WITHOUT 
 6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 8  # details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 51 
12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
13  # 
14  # Copyright Buildbot Team Members 
15   
16   
17  # copied from foolscap 
18   
19  from twisted.internet import reactor, defer 
20  from twisted.python import log 
21   
22 -class _SimpleCallQueue(object):
23 24 _reactor = reactor 25
26 - def __init__(self):
27 self._events = [] 28 self._flushObservers = [] 29 self._timer = None 30 self._in_turn = False
31
32 - def append(self, cb, args, kwargs):
33 self._events.append((cb, args, kwargs)) 34 if not self._timer: 35 self._timer = self._reactor.callLater(0, self._turn)
36
37 - def _turn(self):
38 self._timer = None 39 self._in_turn = True 40 # flush all the messages that are currently in the queue. If anything 41 # gets added to the queue while we're doing this, those events will 42 # be put off until the next turn. 43 events, self._events = self._events, [] 44 for cb, args, kwargs in events: 45 try: 46 cb(*args, **kwargs) 47 except: 48 log.err() 49 self._in_turn = False 50 if self._events and not self._timer: 51 self._timer = self._reactor.callLater(0, self._turn) 52 if not self._events: 53 observers, self._flushObservers = self._flushObservers, [] 54 for o in observers: 55 o.callback(None)
56
57 - def flush(self):
58 if not self._events and not self._in_turn: 59 return defer.succeed(None) 60 d = defer.Deferred() 61 self._flushObservers.append(d) 62 return d
63 64 65 _theSimpleQueue = _SimpleCallQueue() 66
67 -def eventually(cb, *args, **kwargs):
68 _theSimpleQueue.append(cb, args, kwargs)
69 70
71 -def fireEventually(value=None):
72 d = defer.Deferred() 73 eventually(d.callback, value) 74 return d
75
76 -def flushEventualQueue(_ignored=None):
77 return _theSimpleQueue.flush()
78
79 -def _setReactor(r=None):
80 # This sets the reactor used to schedule future events to r. If r is None 81 # (the default), the reactor is reset to its default value. 82 # This should only be used for unit tests. 83 if r is None: 84 r = reactor 85 _theSimpleQueue._reactor = r
86