My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""
Port of Queue.Queue from the python standard library.
"""
__all__ = ['Full', 'Empty', 'Queue']
import collections
import events

from util import priority


class Full(Exception):
pass
class Empty(Exception):
pass
class QGet(events.TimedOperation):
"A operation for the queue get call."
__slots__ = ('queue', 'block', 'caller', 'result', 'waiting')
def __init__(self, queue, block, **kws):
super(QGet, self).__init__(**kws)
self.queue = queue
self.block = block
self.caller = None
self.result = None
self.waiting = False

def finalize(self, sched):
super(QGet, self).finalize(sched)
return self.result

def cleanup(self, sched, coro):
if self.waiting:
self.queue.waiting_gets.remove(self)
return True

def process(self, sched, coro):
super(QGet, self).process(sched, coro)
self.caller = coro
if self.queue._empty():
if self.block:
self.queue.waiting_gets.append(self)
self.waiting = True
else:
raise Empty
else:
self.result = self.queue._get()
if self.queue.waiting_puts:
while not self.queue.full():
putop = self.queue.waiting_puts.popleft()
putop.waiting = False
self.queue._put(putop.item)
if putop.prio & priority.CORO:
if putop.prio & priority.OP:
sched.active.appendleft((self, coro))
else:
sched.active.append((self, coro))

return putop, putop.caller
else:
if putop.prio & priority.OP:
sched.active.appendleft((putop, putop.caller))
else:
sched.active.append((putop, putop.caller))
return self, coro
def __repr__(self):
return "<%s@%X caller:%s block:%s result:%s>" % (
self.__class__.__name__,
id(self),
self.caller,
self.block,
self.result
)

class QPut(events.TimedOperation):
"A operation for the queue put call."
__slots__ = ('queue', 'item', 'block', 'caller', 'result', 'waiting')
def __init__(self, queue, item, block, **kws):
super(QPut, self).__init__(**kws)
self.queue = queue
self.item = item
self.block = block
self.caller = None
self.waiting = False

def cleanup(self, sched, coro):
if self.waiting:
self.queue.waiting_puts.remove(self)
return True

def process(self, sched, coro):
super(QPut, self).process(sched, coro)
self.caller = coro
if self.queue._full():
if self.block:
self.queue.unfinished_tasks += 1
self.queue.waiting_puts.append(self)
self.waiting = True
else:
raise Full
else:
self.queue.unfinished_tasks += 1
if self.queue.waiting_gets:
getop = self.queue.waiting_gets.popleft()
getop.result = self.item
getop.waiting = False
if self.prio:
if self.prio & priority.CORO:
sched.active.appendleft((self, coro))
else:
sched.active.append((self, coro))
return getop, getop.caller
else:
if getop.prio:
sched.active.appendleft((getop, getop.caller))
else:
sched.active.append((getop, getop.caller))
return self, coro

else:
self.queue._put(self.item)
return self, coro
def __repr__(self):
return "<%s@%X caller:%s block:%s item:%s>" % (
self.__class__.__name__,
id(self),
self.caller,
self.block,
self.item
)


class QDone(events.Operation):
"A operation for the queue done_task call"
__slots__ = ('queue',)

def __init__(self, queue, **kws):
super(QDone, self).__init__(**kws)
self.queue = queue

def process(self, sched, coro):
super(QDone, self).process(sched, coro)
if self.queue.joinees:
if self.prio & priority.OP:
sched.active.extendleft(self.queue.joinees)
else:
sched.active.extend(self.queue.joinees)
return self, coro

class QJoin(events.Operation):
"A operation for the queue join call."
__slots__ = ('queue',)

def __init__(self, queue, **kws):
super(QJoin, self).__init__(**kws)
self.queue = queue

def process(self, sched, coro):
super(QJoin, self).process(sched, coro)
if self.queue.unfinished_tasks == 0:
return self, coro
else:
self.queue.joinees.append( (self, coro) )

class Queue:
"""This class attempts to mimic the exact functionality of the
python standard library Queue.Queue class, but with a coroutine context:

* the queue calls return coroutine operations

So, to use this you write someting like:

.. sourcecode:: python

@coroutine
def foo():
q = cogen.core.queue.Queue(<size>)
yield q.put(123)
val = yield q.get()
"""
def __init__(self, maxsize=0):
self._init(maxsize)
self.waiting_puts = collections.deque()
self.waiting_gets = collections.deque()
self.unfinished_tasks = 0
self.joinees = []

def __repr__(self):
return "<%s %s wput:%s wget:%s>" % (
self.__class__,
self._repr(),
self.waiting_puts,
self.waiting_gets
)

def task_done(self, **kws):
"""Indicate that a formerly enqueued task is complete.

Used by Queue consumer threads. For each get() used to fetch a task,
a subsequent call to task_done() tells the queue that the processing
on the task is complete.

If a join() is currently blocking, it will resume when all items
have been processed (meaning that a task_done() call was received
for every item that had been put() into the queue).

Raises a ValueError if called more times than there were items
placed in the queue.
"""
unfinished = self.unfinished_tasks - 1
op = None
if unfinished <= 0:
if unfinished < 0:
raise ValueError('task_done() called too many times')
op = QDone(self, **kws)
self.unfinished_tasks = unfinished
return op

def join(self):
"""Blocks until all items in the Queue have been gotten and processed.

The count of unfinished tasks goes up whenever an item is added to the
queue. The count goes down whenever a consumer thread calls task_done()
to indicate the item was retrieved and all work on it is complete.

When the count of unfinished tasks drops to zero, join() unblocks.
"""
if self.unfinished_tasks:
return QJoin(self)

def qsize(self):
"""Return the approximate size of the queue (not reliable!)."""
return self._qsize()

def empty(self):
"""Return True if the queue is empty, False otherwise (not reliable!)."""
return self._empty()

def full(self):
"""Return True if the queue is full, False otherwise (not reliable!)."""
return self._full()

def put(self, item, block=True, **kws):
"""Put an item into the queue.

If optional args 'block' is true and 'timeout' is None (the default),
block if necessary until a free slot is available. If 'timeout' is
a positive number, it blocks at most 'timeout' seconds and raises
the Full exception if no free slot was available within that time.
Otherwise ('block' is false), put an item on the queue if a free slot
is immediately available, else raise the Full exception ('timeout'
is ignored in that case).
"""
return QPut(self, item, block, **kws)

def put_nowait(self, item):
"""Put an item into the queue without blocking.

Only enqueue the item if a free slot is immediately available.
Otherwise raise the Full exception.
"""
return self.put(item, False)

def get(self, block=True, **kws):
"""Remove and return an item from the queue.

If optional args 'block' is true and 'timeout' is None (the default),
block if necessary until an item is available. If 'timeout' is
a positive number, it blocks at most 'timeout' seconds and raises
the Empty exception if no item was available within that time.
Otherwise ('block' is false), return an item if one is immediately
available, else raise the Empty exception ('timeout' is ignored
in that case).
"""
return QGet(self, block, **kws)

def get_nowait(self):
"""Remove and return an item from the queue without blocking.

Only get an item if one is immediately available. Otherwise
raise the Empty exception.
"""
return self.get(False)


# Override these methods to implement other queue organizations
# (e.g. stack or priority queue).
# These will only be called with appropriate locks held

# Initialize the queue representation
def _init(self, maxsize):
self.maxsize = maxsize
self.queue = collections.deque()

def _qsize(self):
return len(self.queue)

# Check whether the queue is empty
def _empty(self):
return not self.queue

# Check whether the queue is full
def _full(self):
return self.maxsize > 0 and len(self.queue) == self.maxsize

# Put a new item in the queue
def _put(self, item):
self.queue.append(item)

# Get an item from the queue
def _get(self):
return self.queue.popleft()

def _repr(self):
return repr(self.queue)

Show details Hide details

Change log

r588 by ionel.mc on Apr 26, 2009   Diff
Removed trailing spaces. Fixed
inconsistent end of line format.

Merged from amcnabb8's branch.
Go to: 
Project members, sign in to write a code review

Older revisions

r586 by ionel.mc on Apr 26, 2009   Diff
added an 'sched' param to Operation's
finalize method thourgh all the code
to support additional cleanup (mainly
for TimedOperation to remove itself
from the timeouts heapq on
...
r488 by ionel.mc on Oct 15, 2008   Diff
converted __slots__ from lists to
tuples
r379 by ionel.mc on May 02, 2008   Diff
changed all the docstrings to
restructuredtext
All revisions of this file

File info

Size: 10675 bytes, 314 lines
Hosted by Google Code