~ubuntu-branches/ubuntu/vivid/python-gevent/vivid

« back to all changes in this revision

Viewing changes to gevent/queue.py

  • Committer: Bazaar Package Importer
  • Author(s): Örjan Persson
  • Date: 2011-05-17 16:43:20 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517164320-844bxblhlra65dml
Tags: 0.13.6-1
New upstream version (Closes: #601863).

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
Another interesting difference is that :meth:`Queue.qsize`, :meth:`Queue.empty`, and
14
14
:meth:`Queue.full` *can* be used as indicators of whether the subsequent :meth:`Queue.get`
15
15
or :meth:`Queue.put` will not block.
 
16
 
 
17
Additionally, queues in this module implement iterator protocol. Iterating over queue
 
18
means repeatedly calling :meth:`get <Queue.get>` until :meth:`get <Queue.get>` returns ``StopIteration``.
 
19
 
 
20
    >>> queue = gevent.queue.Queue()
 
21
    >>> queue.put(1)
 
22
    >>> queue.put(2)
 
23
    >>> queue.put(StopIteration)
 
24
    >>> for item in queue:
 
25
    ...    print item
 
26
    1
 
27
    2
16
28
"""
17
29
 
18
30
import sys
126
138
            try:
127
139
                if self.getters:
128
140
                    self._schedule_unlock()
129
 
                result = waiter.wait()
 
141
                result = waiter.get()
130
142
                assert result is waiter, "Invalid switch into Queue.put: %r" % (result, )
131
143
                if waiter.item is not _NONE:
132
144
                    self._put(item)
175
187
                self.getters.add(waiter)
176
188
                if self.putters:
177
189
                    self._schedule_unlock()
178
 
                return waiter.wait()
 
190
                return waiter.get()
179
191
            finally:
180
192
                self.getters.discard(waiter)
181
193
                timeout.cancel()
208
220
                        getter = self.getters.pop()
209
221
                        if getter:
210
222
                            item = putter.item
211
 
                            putter.item = _NONE # this makes greenlet calling put() not to call _put() again
 
223
                            putter.item = _NONE  # this makes greenlet calling put() not to call _put() again
212
224
                            self._put(item)
213
225
                            item = self._get()
214
226
                            getter.switch(item)
221
233
                else:
222
234
                    break
223
235
        finally:
224
 
            self._event_unlock = None # QQQ maybe it's possible to obtain this info from libevent?
 
236
            self._event_unlock = None  # QQQ maybe it's possible to obtain this info from libevent?
225
237
            # i.e. whether this event is pending _OR_ currently executing
226
238
        # testcase: 2 greenlets: while True: q.put(q.get()) - nothing else has a change to execute
227
239
        # to avoid this, schedule unlock with timer(0, ...) once in a while
231
243
            self._event_unlock = core.active_event(self._unlock)
232
244
            # QQQ re-activate event (with event_active libevent call) instead of creating a new one each time
233
245
 
 
246
    def __iter__(self):
 
247
        return self
 
248
 
 
249
    def next(self):
 
250
        result = self.get()
 
251
        if result is StopIteration:
 
252
            raise result
 
253
        return result
 
254
 
234
255
 
235
256
class ItemWaiter(Waiter):
236
257
    __slots__ = ['item']
277
298
        Queue.__init__(self, maxsize)
278
299
        self.unfinished_tasks = 0
279
300
        self._cond = Event()
 
301
        self._cond.set()
280
302
 
281
303
    def _format(self):
282
304
        result = Queue._format(self)
315
337
        unfinished tasks drops to zero, :meth:`join` unblocks.
316
338
        '''
317
339
        self._cond.wait()
318