-
Notifications
You must be signed in to change notification settings - Fork 11
/
stream.py
1284 lines (1030 loc) · 34.4 KB
/
stream.py
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Lazily-evaluated, parallelizable pipeline.
Overview
========
Streams are iterables with a pipelining mechanism to enable
data-flow programming and easy parallelization.
The idea is to take the output of a function that turn an iterable into
another iterable and plug that as the input of another such function.
While you can already do this using function composition, this package
provides an elegant notation for it by overloading the '>>' operator.
This approach focuses the programming on processing streams of data, step
by step. A pipeline usually starts with a producer, then passes through
a number of filters. Multiple streams can be branched and combined.
Finally, the output is fed to an accumulator, which can be any function
of one iterable argument.
Producers: anything iterable
+ from this module: seq, gseq, repeatcall, chaincall
Filters:
+ by index: take, drop, takei, dropi
+ by condition: filter, takewhile, dropwhile
+ by transformation: apply, map, fold
+ by combining streams: prepend, tee
+ for special purpose: chop, cut, flatten
Accumulators: item, maximum, minimum, reduce
+ from Python: list, sum, dict, max, min ...
Values are computed only when an accumulator forces some or all evaluation
(not when the stream are set up).
Parallelization
===============
All parts of a pipeline can be parallelized using multiple threads or processes.
When a producer is doing blocking I/O, it is possible to use a ThreadedFeeder
or ForkedFeeder to improve performance. The feeder will start a thread or a
process to run the producer and feed generated items back to the pipeline, thus
minimizing the time that the whole pipeline has to wait when the producer is
blocking in system calls.
If the order of processing does not matter, an ThreadPool or ProcessPool
can be used. They both utilize a number of workers in other theads
or processes to work on items pulled from the input stream. Their output
are simply iterables respresented by the pool objects which can be used in
pipelines. Alternatively, an Executor can perform fine-grained, concurrent job
control over a thread/process pool.
Multiple streams can be piped to a single PCollector or QCollector, which
will gather generated items whenever they are avaiable. PCollectors
can collect from ForkedFeeder's or ProcessPool's (via system pipes) and
QCollector's can collect from ThreadedFeeder's and ThreadPool's (via queues).
PSorter and QSorter are also collectors, but given multiples sorted input
streams (low to high), a Sorter will output items in sorted order.
Using multiples Feeder's and Collector's, one can implement many parallel
processing patterns: fan-in, fan-out, many-to-many map-reduce, etc.
Articles
========
Articles written about this module by the author can be retrieved from
<http://blog.onideas.ws/tag/project:stream.py>.
"""
from __future__ import with_statement
import __builtin__
import copy
import collections
import heapq
import itertools
import operator
import Queue
import re
import select
import sys
import threading
import time
from operator import itemgetter, attrgetter
zip = itertools.izip
try:
import multiprocessing
import multiprocessing.queues
_nCPU = multiprocessing.cpu_count()
except ImportError:
_nCPU = 1
try:
Iterable = collections.Iterable
except AttributeError:
Iterable = object
try:
next
except NameError:
def next(iterator):
return iterator.next()
try:
from operator import methodcaller
except ImportError:
def methodcaller(methodname, *args, **kwargs):
return lambda o: getattr(o, methodname)(*args, **kwargs)
__version__ = '0.8'
#_____________________________________________________________________
# Base class
class BrokenPipe(Exception):
pass
class Stream(Iterable):
"""A stream is both a lazy list and an iterator-processing function.
The lazy list is represented by the attribute 'iterator'.
The iterator-processing function is represented by the method
__call__(iterator), which should return a new iterator
representing the output of the Stream.
By default, __call__(iterator) chains iterator with self.iterator,
appending itself to the input stream in effect.
__pipe__(inpipe) defines the connection mechanism between Stream objects.
By default, it replaces self.iterator with the iterator returned by
__call__(iter(inpipe)).
A Stream subclass will usually implement __call__, unless it is an
accumulator and will not return a Stream, in which case it will need to
implement __pipe__.
The `>>` operator works as follow: the expression `a >> b` means
`b.__pipe__(a) if hasattr(b, '__pipe__') else b(a)`.
>>> [1, 2, 3] >> Stream([4, 5, 6]) >> list
[1, 2, 3, 4, 5, 6]
"""
def __init__(self, iterable=None):
"""Make a Stream object from an iterable."""
self.iterator = iter(iterable if iterable else [])
def __iter__(self):
return self.iterator
def __call__(self, iterator):
"""Append to the end of iterator."""
return itertools.chain(iterator, self.iterator)
def __pipe__(self, inpipe):
self.iterator = self.__call__(iter(inpipe))
return self
@staticmethod
def pipe(inpipe, outpipe):
"""Connect inpipe and outpipe. If outpipe is not a Stream instance,
it should be an function callable on an iterable.
"""
if hasattr(outpipe, '__pipe__'):
return outpipe.__pipe__(inpipe)
elif hasattr(outpipe, '__call__'):
return outpipe(inpipe)
else:
raise BrokenPipe('No connection mechanism defined')
def __rshift__(self, outpipe):
return Stream.pipe(self, outpipe)
def __rrshift__(self, inpipe):
return Stream.pipe(inpipe, self)
def extend(self, outpipe):
"""Similar to __pipe__, except that outpipe must be a Stream, in
which case self.iterator will be modified in-place by calling
outpipe.__call__ on it.
"""
self.iterator = outpipe.__call__(self.iterator)
return self
def __repr__(self):
return 'Stream(%s)' % repr(self.iterator)
#_______________________________________________________________________
# Process streams by element indices
class take(Stream):
"""Take the firts n items of the input stream, return a Stream.
>>> seq(1, 2) >> take(10)
Stream([1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
"""
def __init__(self, n):
"""n: the number of elements to be taken"""
super(take, self).__init__()
self.n = n
self.items = []
def __call__(self, iterator):
self.items = list(itertools.islice(iterator, self.n))
return iter(self.items)
def __repr__(self):
return 'Stream(%s)' % repr(self.items)
negative = lambda x: x and x < 0 ### since None < 0 == True
class itemtaker(Stream):
"""Slice the input stream, return a list.
>>> i = itertools.count()
>>> i >> item[:10:2]
[0, 2, 4, 6, 8]
>>> i >> item[:5]
[10, 11, 12, 13, 14]
>>> xrange(20) >> item[::-2]
[19, 17, 15, 13, 11, 9, 7, 5, 3, 1]
"""
def __init__(self, key=None):
self.key = key
@staticmethod
def __getitem__(key):
if (type(key) is int) or (type(key) is slice):
return itemtaker(key)
else:
raise TypeError('key must be an integer or a slice')
def __pipe__(self, inpipe):
i = iter(inpipe)
if type(self.key) is int:
## just one item is needed
if self.key >= 0:
# throw away self.key items
collections.deque(itertools.islice(i, self.key), maxlen=0)
return next(i)
else:
# keep the last -self.key items
# since we don't know beforehand when the stream stops
n = -self.key if self.key else 1
items = collections.deque(itertools.islice(i, None), maxlen=n)
if items:
return items[-n]
else:
return []
else:
## a list is needed
if negative(self.key.stop) or negative(self.key.start) \
or not (self.key.start or self.key.stop) \
or (not self.key.start and negative(self.key.step)) \
or (not self.key.stop and not negative(self.key.step)):
# force all evaluation
items = [x for x in i]
else:
# force some evaluation
if negative(self.key.step):
stop = self.key.start
else:
stop = self.key.stop
items = list(itertools.islice(i, stop))
return items[self.key]
def __repr__(self):
return '<itemtaker at %s>' % hex(id(self))
item = itemtaker()
class takei(Stream):
"""Take elements of the input stream by indices.
>>> seq() >> takei(xrange(2, 43, 4)) >> list
[2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42]
"""
def __init__(self, indices):
"""indices: an iterable of indices to be taken, should yield
non-negative integers in monotonically increasing order
"""
super(takei, self).__init__()
self.indexiter = iter(indices)
def __call__(self, iterator):
def itaker():
old_idx = -1
idx = next(self.indexiter) # next value to yield
counter = seq()
while 1:
c = next(counter)
elem = next(iterator)
while idx <= old_idx: # ignore bad values
idx = next(self.indexiter)
if c == idx:
yield elem
old_idx = idx
idx = next(self.indexiter)
return itaker()
class drop(Stream):
"""Drop the first n elements of the input stream.
>>> seq(0, 2) >> drop(1) >> take(5)
Stream([2, 4, 6, 8, 10])
"""
def __init__(self, n):
"""n: the number of elements to be dropped"""
super(drop, self).__init__()
self.n = n
def __call__(self, iterator):
collections.deque(itertools.islice(iterator, self.n), maxlen=0)
return iterator
class dropi(Stream):
"""Drop elements of the input stream by indices.
>>> seq() >> dropi(seq(0,3)) >> item[:10]
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14]
"""
def __init__(self, indices):
"""indices: an iterable of indices to be dropped, should yield
non-negative integers in monotonically increasing order
"""
super(dropi, self).__init__()
self.indexiter = iter(indices)
def __call__(self, iterator):
def idropper():
counter = seq()
def try_next_idx():
## so that the stream keeps going
## after the discard iterator is exhausted
try:
return next(self.indexiter), False
except StopIteration:
return -1, True
old_idx = -1
idx, exhausted = try_next_idx() # next value to discard
while 1:
c = next(counter)
elem = next(iterator)
while not exhausted and idx <= old_idx: # ignore bad values
idx, exhausted = try_next_idx()
if c != idx:
yield elem
elif not exhausted:
old_idx = idx
idx, exhausted = try_next_idx()
return idropper()
#_______________________________________________________________________
# Process streams with functions and higher-order ones
class Processor(Stream):
"""A decorator to turn an iterator-processing function into
a Stream processor object.
"""
def __init__(self, function):
"""function: an iterator-processing function, one that takes an
iterator and return an iterator
"""
super(Processor, self).__init__()
self.function = function
def __call__(self, iterator):
return self.function(iterator)
class apply(Stream):
"""Invoke a function using each element of the input stream unpacked as
its argument list, a la itertools.starmap.
>>> vectoradd = lambda u,v: zip(u, v) >> apply(lambda x,y: x+y) >> list
>>> vectoradd([1, 2, 3], [4, 5, 6])
[5, 7, 9]
"""
def __init__(self, function):
"""function: to be called with each stream element unpacked as its
argument list
"""
super(apply, self).__init__()
self.function = function
def __call__(self, iterator):
return itertools.starmap(self.function, iterator)
class map(Stream):
"""Invoke a function using each element of the input stream as its only
argument, a la itertools.imap.
>>> square = lambda x: x*x
>>> range(10) >> map(square) >> list
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
"""
def __init__(self, function):
"""function: to be called with each stream element as its
only argument
"""
super(map, self).__init__()
self.function = function
def __call__(self, iterator):
return itertools.imap(self.function, iterator)
class filter(Stream):
"""Filter the input stream, selecting only values which evaluates to True
by the given function, a la itertools.ifilter.
>>> even = lambda x: x%2 == 0
>>> range(10) >> filter(even) >> list
[0, 2, 4, 6, 8]
"""
def __init__(self, function):
"""function: to be called with each stream element as its
only argument
"""
super(filter, self).__init__()
self.function = function
def __call__(self, iterator):
return itertools.ifilter(self.function, iterator)
class takewhile(Stream):
"""Take items from the input stream that come before the first item to
evaluate to False by the given function, a la itertools.takewhile.
"""
def __init__(self, function):
"""function: to be called with each stream element as its
only argument
"""
super(takewhile, self).__init__()
self.function = function
def __call__(self, iterator):
return itertools.takewhile(self.function, iterator)
class dropwhile(Stream):
"""Drop items from the input stream that come before the first item to
evaluate to False by the given function, a la itertools.dropwhile.
"""
def __init__(self, function):
"""function: to be called with each stream element as its
only argument
"""
super(dropwhile, self).__init__()
self.function = function
def __call__(self, iterator):
return itertools.dropwhile(self.function, iterator)
class fold(Stream):
"""Combines the elements of the input stream by applying a function of two
argument to a value and each element in turn. At each step, the value is
set to the value returned by the function, thus it is, in effect, an
accumulation.
Intermediate values are yielded (similar to Haskell `scanl`).
This example calculate partial sums of the series 1 + 1/2 + 1/4 +...
>>> gseq(0.5) >> fold(operator.add) >> item[:5]
[1, 1.5, 1.75, 1.875, 1.9375]
"""
def __init__(self, function, initval=None):
super(fold, self).__init__()
self.function = function
self.initval = initval
def __call__(self, iterator):
def folder():
if self.initval:
accumulated = self.initval
else:
accumulated = next(iterator)
while 1:
yield accumulated
val = next(iterator)
accumulated = self.function(accumulated, val)
return folder()
#_____________________________________________________________________
# Special purpose stream processors
class chop(Stream):
"""Chop the input stream into segments of length n.
>>> range(10) >> chop(3) >> list
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
"""
def __init__(self, n):
"""n: the length of the segments"""
super(chop, self).__init__()
self.n = n
def __call__(self, iterator):
def chopper():
while 1:
s = iterator >> item[:self.n]
if s:
yield s
else:
break
return chopper()
class itemcutter(map):
"""Slice each element of the input stream.
>>> [range(10), range(10, 20)] >> cut[::2] >> list
[[0, 2, 4, 6, 8], [10, 12, 14, 16, 18]]
"""
def __init__(self, *args):
super(itemcutter, self).__init__( methodcaller('__getitem__', *args) )
@classmethod
def __getitem__(cls, args):
return cls(args)
def __repr__(self):
return '<itemcutter at %s>' % hex(id(self))
cut = itemcutter()
class flattener(Stream):
"""Flatten a nested stream of arbitrary depth.
>>> (xrange(i) for i in seq(step=3)) >> flatten >> item[:18]
[0, 1, 2, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7, 8]
"""
@staticmethod
def __call__(iterator):
def flatten():
## Maintain a LIFO stack of iterators
stack = []
i = iterator
while True:
try:
e = next(i)
if hasattr(e, "__iter__") and not isinstance(e, basestring):
stack.append(i)
i = iter(e)
else:
yield e
except StopIteration:
try:
i = stack.pop()
except IndexError:
break
return flatten()
def __repr__(self):
return '<flattener at %s>' % hex(id(self))
flatten = flattener()
#_______________________________________________________________________
# Combine multiple streams
class prepend(Stream):
"""Inject values at the beginning of the input stream.
>>> seq(7, 7) >> prepend(xrange(0, 10, 2)) >> item[:10]
[0, 2, 4, 6, 8, 7, 14, 21, 28, 35]
"""
def __call__(self, iterator):
return itertools.chain(self.iterator, iterator)
class tee(Stream):
"""Make a T-split of the input stream.
>>> foo = filter(lambda x: x%3==0)
>>> bar = seq(0, 2) >> tee(foo)
>>> bar >> item[:5]
[0, 2, 4, 6, 8]
>>> foo >> item[:5]
[0, 6, 12, 18, 24]
"""
def __init__(self, named_stream):
"""named_stream: a Stream object toward which the split branch
will be piped.
"""
super(tee, self).__init__()
self.named_stream = named_stream
def __pipe__(self, inpipe):
branch1, branch2 = itertools.tee(iter(inpipe))
self.iterator = branch1
Stream.pipe(branch2, self.named_stream)
return self
#_____________________________________________________________________
# _iterqueue and _iterrecv
def _iterqueue(queue):
# Turn a either a threading.Queue or a multiprocessing.queues.SimpleQueue
# into an thread-safe iterator which will exhaust when StopIteration is
# put into it.
while 1:
item = queue.get()
if item is StopIteration:
# Re-broadcast, in case there is another listener blocking on
# queue.get(). That listener will receive StopIteration and
# re-broadcast to the next one in line.
try:
queue.put(StopIteration)
except IOError:
# Could happen if the Queue is based on a system pipe,
# and the other end was closed.
pass
break
else:
yield item
def _iterrecv(pipe):
# Turn a the receiving end of a multiprocessing.Connection object
# into an iterator which will exhaust when StopIteration is
# put into it. _iterrecv is NOT safe to use by multiple threads.
while 1:
try:
item = pipe.recv()
except EOFError:
break
else:
if item is StopIteration:
break
else:
yield item
#_____________________________________________________________________
# Threaded/forked feeder
class ThreadedFeeder(Iterable):
def __init__(self, generator, *args, **kwargs):
"""Create a feeder that start the given generator with
*args and **kwargs in a separate thread. The feeder will
act as an eagerly evaluating proxy of the generator.
The feeder can then be iter()'ed over by other threads.
This should improve performance when the generator often
blocks in system calls.
"""
self.outqueue = Queue.Queue()
def feeder():
i = generator(*args, **kwargs)
while 1:
try:
self.outqueue.put(next(i))
except StopIteration:
self.outqueue.put(StopIteration)
break
self.thread = threading.Thread(target=feeder)
self.thread.start()
def __iter__(self):
return _iterqueue(self.outqueue)
def join(self):
self.thread.join()
def __repr__(self):
return '<ThreadedFeeder at %s>' % hex(id(self))
class ForkedFeeder(Iterable):
def __init__(self, generator, *args, **kwargs):
"""Create a feeder that start the given generator with
*args and **kwargs in a child process. The feeder will
act as an eagerly evaluating proxy of the generator.
The feeder can then be iter()'ed over by other processes.
This should improve performance when the generator often
blocks in system calls. Note that serialization could
be costly.
"""
self.outpipe, inpipe = multiprocessing.Pipe(duplex=False)
def feed():
i = generator(*args, **kwargs)
while 1:
try:
inpipe.send(next(i))
except StopIteration:
inpipe.send(StopIteration)
break
self.process = multiprocessing.Process(target=feed)
self.process.start()
def __iter__(self):
return _iterrecv(self.outpipe)
def join(self):
self.process.join()
def __repr__(self):
return '<ForkedFeeder at %s>' % hex(id(self))
#_____________________________________________________________________
# Asynchronous stream processing using a pool of threads or processes
class ThreadPool(Stream):
"""Work on the input stream asynchronously using a pool of threads.
>>> range(10) >> ThreadPool(map(lambda x: x*x)) >> sum
285
The pool object is an iterable over the output values. If an
input value causes an Exception to be raised, the tuple (value,
exception) is put into the pool's `failqueue`. The attribute
`failure` is a thead-safe iterator over the `failqueue`.
See also: Executor
"""
def __init__(self, function, poolsize=_nCPU, args=[], kwargs={}):
"""function: an iterator-processing function, one that takes an
iterator and return an iterator
"""
super(ThreadPool, self).__init__()
self.function = function
self.inqueue = Queue.Queue()
self.outqueue = Queue.Queue()
self.failqueue = Queue.Queue()
self.failure = Stream(_iterqueue(self.failqueue))
self.closed = False
def work():
input, dupinput = itertools.tee(_iterqueue(self.inqueue))
output = self.function(input, *args, **kwargs)
while 1:
try:
self.outqueue.put(next(output))
next(dupinput)
except StopIteration:
break
except Exception, e:
self.failqueue.put((next(dupinput), e))
self.worker_threads = []
for _ in range(poolsize):
t = threading.Thread(target=work)
self.worker_threads.append(t)
t.start()
def cleanup():
# Wait for all workers to finish,
# then signal the end of outqueue and failqueue.
for t in self.worker_threads:
t.join()
self.outqueue.put(StopIteration)
self.failqueue.put(StopIteration)
self.closed = True
self.cleaner_thread = threading.Thread(target=cleanup)
self.cleaner_thread.start()
self.iterator = _iterqueue(self.outqueue)
def __call__(self, inpipe):
if self.closed:
raise BrokenPipe('All workers are dead, refusing to summit jobs. '
'Use another Pool.')
def feed():
for item in inpipe:
self.inqueue.put(item)
self.inqueue.put(StopIteration)
self.feeder_thread = threading.Thread(target=feed)
self.feeder_thread.start()
return self.iterator
def join(self):
self.cleaner_thread.join()
def __repr__(self):
return '<ThreadPool(poolsize=%s) at %s>' % (self.poolsize, hex(id(self)))
class ProcessPool(Stream):
"""Work on the input stream asynchronously using a pool of processes.
>>> range(10) >> ProcessPool(map(lambda x: x*x)) >> sum
285
The pool object is an iterable over the output values. If an
input value causes an Exception to be raised, the tuple (value,
exception) is put into the pool's `failqueue`. The attribute
`failure` is a thead-safe iterator over the `failqueue`.
See also: Executor
"""
def __init__(self, function, poolsize=_nCPU, args=[], kwargs={}):
"""function: an iterator-processing function, one that takes an
iterator and return an iterator
"""
super(ProcessPool, self).__init__()
self.function = function
self.poolsize = poolsize
self.inqueue = multiprocessing.queues.SimpleQueue()
self.outqueue = multiprocessing.queues.SimpleQueue()
self.failqueue = multiprocessing.queues.SimpleQueue()
self.failure = Stream(_iterqueue(self.failqueue))
self.closed = False
def work():
input, dupinput = itertools.tee(_iterqueue(self.inqueue))
output = self.function(input, *args, **kwargs)
while 1:
try:
self.outqueue.put(next(output))
next(dupinput)
except StopIteration:
break
except Exception, e:
self.failqueue.put((next(dupinput), e))
self.worker_processes = []
for _ in range(self.poolsize):
p = multiprocessing.Process(target=work)
self.worker_processes.append(p)
p.start()
def cleanup():
# Wait for all workers to finish,
# then signal the end of outqueue and failqueue.
for p in self.worker_processes:
p.join()
self.outqueue.put(StopIteration)
self.failqueue.put(StopIteration)
self.closed = True
self.cleaner_thread = threading.Thread(target=cleanup)
self.cleaner_thread.start()
self.iterator = _iterqueue(self.outqueue)
def __call__(self, inpipe):
if self.closed:
raise BrokenPipe('All workers are dead, refusing to summit jobs. '
'Use another Pool.')
def feed():
for item in inpipe:
self.inqueue.put(item)
self.inqueue.put(StopIteration)
self.feeder_thread = threading.Thread(target=feed)
self.feeder_thread.start()
return self.iterator
def join(self):
self.cleaner_thread.join()
def __repr__(self):
return '<ProcessPool(poolsize=%s) at %s>' % (self.poolsize, hex(id(self)))
class Executor(object):
"""Provide a fine-grained level of control over a ThreadPool or ProcessPool.
The constructor takes a pool class and arguments to its constructor::
>>> executor = Executor(ThreadPool, map(lambda x: x*x))
Job ids are returned when items are submitted::
>>> executor.submit(*range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> executor.submit('foo')
10
A call to close() ends jobs submission. Workers threads/processes
are now allowed to terminate after all jobs are completed::
>>> executor.close()
The `result` and `failure` attributes are Stream instances and
thus iterable. The returned iterators behave as follow: their
next() calls will block until a next output is available, or
raise StopIteration if there is no more output. Thus we could use
the attributes `result` and `failure` like any other iterables::
>>> set(executor.result) == set([0, 1, 4, 9, 16, 25, 36, 49, 64, 81])
True
>>> list(executor.failure)
[('foo', TypeError("can't multiply sequence by non-int of type 'str'",))]
"""
def __init__(self, poolclass, function, poolsize=_nCPU, args=[], kwargs={}):
def process_job_id(input):
input, dupinput = itertools.tee(input)
id = iter(dupinput >> cut[0])
input = iter(input >> cut[1])
output = function(input, *args, **kwargs)
for item in output:
yield next(id), item
self.pool = poolclass(process_job_id,
poolsize=poolsize,
args=args,
kwargs=kwargs)
self.jobcount = 0
self._status = []
self.waitqueue = Queue.Queue()
if poolclass is ProcessPool:
self.resultqueue = multiprocessing.queues.SimpleQueue()
self.failqueue = multiprocessing.queues.SimpleQueue()
else:
self.resultqueue = Queue.Queue()
self.failqueue = Queue.Queue()
self.result = Stream(_iterqueue(self.resultqueue))
self.failure = Stream(_iterqueue(self.failqueue))
self.closed = False
self.lock = threading.Lock()
## Acquired to submit and update job statuses.
self.sema = threading.BoundedSemaphore(poolsize)
## Used to throttle transfer from waitqueue to pool.inqueue,
## acquired by input_feeder, released by trackers.
def feed_input():
for id, item in _iterqueue(self.waitqueue):
self.sema.acquire()
with self.lock:
if self._status[id] == 'SUBMITTED':
self.pool.inqueue.put((id, item))
self._status[id] = 'RUNNING'
else:
self.sema.release()
self.pool.inqueue.put(StopIteration)
self.inputfeeder_thread = threading.Thread(target=feed_input)
self.inputfeeder_thread.start()
def track_result():
for id, item in self.pool:
self.sema.release()
with self.lock:
self._status[id] = 'COMPLETED'
self.resultqueue.put(item)
self.resultqueue.put(StopIteration)
self.resulttracker_thread = threading.Thread(target=track_result)
self.resulttracker_thread.start()
def track_failure():
for outval, exception in self.pool.failure:
self.sema.release()
id, item = outval
with self.lock:
self._status[id] = 'FAILED'
self.failqueue.put((item, exception))
self.failqueue.put(StopIteration)
self.failuretracker_thread = threading.Thread(target=track_failure)
self.failuretracker_thread.start()
def submit(self, *items):
"""Return job ids assigned to the submitted items."""
with self.lock:
if self.closed:
raise BrokenPipe('Job submission has been closed.')
id = self.jobcount
self._status += ['SUBMITTED'] * len(items)
self.jobcount += len(items)
for item in items:
self.waitqueue.put((id, item))
id += 1
if len(items) == 1:
return id - 1
else:
return range(id - len(items), id)
def cancel(self, *ids):
"""Try to cancel jobs with associated ids.
Return the actual number of jobs cancelled.
"""