-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreampie.py
714 lines (564 loc) · 19.6 KB
/
streampie.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
import sys
import zlib
import dill
import redis
import random
import inspect
import threading
import traceback
import itertools
import collections
import multiprocessing
try:
# Python2
import Queue as queue
ifilter = itertools.ifilter
imap = itertools.imap
except:
# Python3
import queue
# In python3, filter and map by default return iterators
ifilter = filter
imap = map
def _ifilter_ext(predicate, iterable):
for i, val in enumerate(iterable):
if predicate and predicate(i, val):
yield val
def _iterqueue(queue):
"""
Take a queue and return an iterator over that queue.
"""
while 1:
item = queue.get()
if item is StopIteration:
queue.put(StopIteration)
break
yield item
class Stream():
"""
This is our generic stream class. It is iterable and it overloads the ``>>`` operator
for convenience.
"""
def __init__(self, obj=None):
self.iterator = None
if isinstance(obj, collections.Iterable):
self.iterator = iter(obj)
def __iter__(self):
return self
# For python3
def __next__(self):
return self.next()
def next(self):
return next(self.iterator)
def __rshift__(self, other):
if inspect.isclass(other):
return other(self)
other.iterator = self
other._on_connect()
return Stream(other)
def __rrshift__(self, other):
return Stream(other) >> self
def __repr__(self):
return "Stream(%s)" % repr(self.iterator)
def _on_connect(self):
return NotImplemented
#
# Stream Processors
#
class take(Stream):
def __init__(self, n):
"""
Take the first ``n`` elements, and drop the rest.
>>> range(4) >> take(2) >> list
[0, 1]
"""
Stream.__init__(self)
self.n = n
def __iter__(self):
return itertools.islice(self.iterator, self.n)
class takei(Stream):
def __init__(self, indices):
"""
Take only the elements whose indices are given in the list.
>>> range(4) >> takei([0, 1]) >> list
[0, 1]
"""
Stream.__init__(self)
self.indices = indices
def __iter__(self):
def _filter(i, val):
return i in self.indices
return _ifilter_ext(_filter, self.iterator)
class drop(Stream):
def __init__(self, n):
"""
Drop the first `n` elements, and take the rest.
>>> range(4) >> drop(2) >> list
[2, 3]
"""
Stream.__init__(self)
self.n = n
def __iter__(self):
collections.deque(itertools.islice(self.iterator, self.n))
return self.iterator
class dropi(Stream):
def __init__(self, indices):
"""
Drop only the elements whose indices are given in the ``indices`` list.
>>> range(4) >> dropi([0, 1]) >> list
[2, 3]
"""
Stream.__init__(self)
self.indices = indices
def __iter__(self):
def _filter(i, val):
return not i in self.indices
return _ifilter_ext(_filter, self.iterator)
class chop(Stream):
def __init__(self, n):
"""
Split the stream into ``n``-sized chunks.
>>> range(4) >> chop(2) >> list
[[0, 1], [2, 3]]
"""
Stream.__init__(self)
self.n = n
def __iter__(self):
def _chop():
while 1:
chunk = list(itertools.islice(self.iterator, self.n))
if not chunk:
break
yield chunk
return _chop()
class map(Stream):
def __init__(self, function):
"""
Call the function ``func`` for every element, with the element as input.
>>> square = lambda x: x**2
>>> range(4) >> map(square) >> list
[0, 1, 4, 9]
"""
Stream.__init__(self)
self.function = function
def __iter__(self):
return imap(self.function, self.iterator)
class filter(Stream):
def __init__(self, function):
"""
Return only the elements for which the predicate ``func`` evaluates to ``True``.
>>> even = lambda x: x % 2 == 0
>>> range(4) >> filter(even) >> list
[0, 2]
"""
Stream.__init__(self)
self.function = function
def __iter__(self):
return ifilter(self.function, self.iterator)
class apply(Stream):
def __init__(self, function):
"""
Call the function ``func`` for every element, with the element as arguments.
>>> sum = lambda x,y: x+y
>>> range(4) >> chop(2) >> apply(sum) >> list
[1, 5]
"""
Stream.__init__(self)
self.function = function
def __iter__(self):
return itertools.starmap(self.function, self.iterator)
class takewhile(Stream):
def __init__(self, predicate):
"""
Keep taking elements until the predicate ``func`` is ``True``, then stop.
>>> range(4) >> takewhile(lambda x: x < 3) >> list
[0, 1, 2]
"""
Stream.__init__(self)
self.predicate = predicate
def __iter__(self):
return itertools.takewhile(self.predicate, self.iterator)
class dropwhile(Stream):
def __init__(self, predicate):
"""
Keep dropping elements until the predicate ``func`` is ``True``, then stop.
>>> range(4) >> dropwhile(lambda x: x < 3) >> list
[3]
"""
Stream.__init__(self)
self.predicate = predicate
def __iter__(self):
return itertools.dropwhile(self.predicate, self.iterator)
class prepend(Stream):
def __init__(self, prep_iterator):
"""
Prepend elements to a stream.
>>> range(4) >> prepend([10, 9]) >> list
[10, 9, 0, 1, 2, 3]
"""
Stream.__init__(self)
self.prep_iterator = prep_iterator
def __iter__(self):
return itertools.chain(self.prep_iterator, self.iterator)
class flatten(Stream):
"""
Flatten an arbitrarily-deep list of lists into a single list.
>>> [0,[1,[2,[3]]]] >> flatten() >> list
[0, 1, 2, 3]
"""
def __iter__(self):
def _flatten(iterator):
stack = []
while 1:
try:
item = next(iterator)
if isinstance(item, collections.Iterable):
stack.append(iter(item))
else:
yield item
except StopIteration:
try:
iterator = stack.pop()
except IndexError:
break
return _flatten(self.iterator)
#
# Paralellism
#
class LocalPool(Stream):
def __init__(self, function, poolsize=None, args=[]):
"""
A generic class shared by all local (executed on the same machine) pools.
"""
Stream.__init__(self)
self.function = function
self.poolsize = poolsize
self.args = args
self.pool = []
if self.poolsize == None:
# No prefered poolsize? Use number of cores
self.poolsize = multiprocessing.cpu_count()
def _worker(self, wid):
try:
for val in self.function(wid, _iterqueue(self.in_queue), *self.args):
self.out_queue.put(val)
except:
# Catch all exceptions and just print them, but keep working
traceback.print_exc()
def _control(self):
# Move all data from the iterator to the input queue
for val in self.iterator:
self.in_queue.put(val)
# Last item in the queue is the stop-signal
self.in_queue.put(StopIteration)
# Wait for all workers to finish
for p in self.pool:
p.join()
# All workers finished, stop the output queue iterator
self.out_queue.put(StopIteration)
def stop(self):
"""
Terminate and wait for all workers to finish.
"""
# Wait for all workers to finish
for p in self.pool:
p.terminate()
def __iter__(self):
return _iterqueue(self.out_queue)
def _on_connect(self):
# Start the control thread
t = threading.Thread(target=self._start_workers)
t.daemon = True
t.start()
class ProcessPool(LocalPool):
def __init__(self, function, poolsize=None, args=[]):
"""
Create a process pool.
:param int function: Function that each worker executes
:param int poolsize: How many workers the pool should make
:param list args: List of arguments to pass to the worker function
A simple that calls the ``sum`` function for every pair of inputs.
>>> def sum(wid, items):
... # wid is the worker id
... # items is an iterator for the inputs to the stream
... for x, y in items:
... yield x + y
>>> range(6) >> chop(2) >> ProcessPool(sum) >> list # doctest: +SKIP
[1, 5, 9]
Note that the order of the output list is not guaranteed, as it depends
in which order the elements were consumed. By default, the class creates
as many workers as there are cores. Here is a more advanced examples
showing ``poolsize`` control and passing additional arguments.
>>> def sum(wid, items, arg1, arg2):
... # arg1 and arg2 are additional arguments passed to the function
... for x, y in items:
... yield x + y
>>> sorted(range(6) >> chop(2) >> ProcessPool(sum, poolsize=8, args=[0, 1]) >> list)
[1, 5, 9]
The function can yield arbitrarily many results. For example, for a single input, two or more
yields can be made.
>>> def sum(wid, items):
... for x, y in items:
... yield x + y
... yield x + y
>>> sorted(range(6) >> chop(2) >> ProcessPool(sum) >> list)
[1, 1, 5, 5, 9, 9]
"""
LocalPool.__init__(self, function, poolsize, args)
self.in_queue = multiprocessing.Queue()
self.out_queue = multiprocessing.Queue()
def _start_workers(self):
# Start the worker processes
for x in range(self.poolsize):
p = multiprocessing.Process(target=self._worker, args=[x])
p.daemon = True
p.start()
self.pool.append(p)
self._control()
class ThreadPool(LocalPool):
def __init__(self, function, poolsize=None, args=[]):
"""
Create a thread pool.
:param int function: Function that each worker executes
:param int poolsize: How many workers the pool should make
:param list args: List of arguments to pass to the worker function
>>> def sum(wid, items):
... # wid is the worker id
... # items is an iterator for the inputs to the stream
... for x, y in items:
... yield x + y
>>> range(6) >> chop(2) >> ThreadPool(sum) >> list # doctest: +SKIP
[1, 5, 9]
"""
LocalPool.__init__(self, function, poolsize, args)
self.in_queue = queue.Queue()
self.out_queue = queue.Queue()
def _start_workers(self):
# Start the worker threads
for x in range(self.poolsize):
t = threading.Thread(target=self._worker, args=[x])
t.daemon = True
t.start()
self.pool.append(t)
self._control()
class StandaloneProcessPool(ProcessPool):
def __init__(self, function, poolsize=None, args=[]):
"""
The standalone process pool is exactly like the :class:`ProcessPool` class, other than
the fact that it does not take any input, but constantly yields output.
:param int function: Function that each worker executes
:param int poolsize: How many workers the pool should make
:param list args: List of arguments to pass to the worker function
To illustrate, here is an example of a worker that constantly returns random numbers.
Since there is no input stream, the pool needs to be manually terminated.
>>> import random
>>> def do_work(wid):
... yield random.random()
>>> pool = StandaloneProcessPool(do_work)
>>> for x, r in enumerate(pool): # doctest: +SKIP
... if x == 2:
... pool.stop()
... break
... print r
0.600151963181
0.144348185086
"""
ProcessPool.__init__(self, function, poolsize, args)
self.iterator = _iterqueue(self.out_queue)
multiprocessing.Process(target=self._start_workers).start()
def _worker(self, wid):
try:
for val in self.function(wid, *self.args):
self.out_queue.put(val)
except:
# Catch all exceptions and just print them, but keep working
traceback.print_exc()
def _control(self):
# Wait for all workers to finish
for p in self.pool:
p.join()
# All workers finished, stop the output queue iterator
self.out_queue.put(StopIteration)
#
# Distributed Paralellism
#
def _dumps(obj):
"""
Serialize and compress an object.
"""
return zlib.compress(dill.dumps(obj))
def _loads(data):
"""
Decompress and deserialize.
"""
return dill.loads(zlib.decompress(data))
class Job:
def __init__(self, target_id, args=[]):
"""
This class is our unit of work. It it fetched by a :class:`Worker`, it's ``target`` is executed, the
result (``ret``) and exception (if any) is stored and sent back to the JobQueue.
:param int target_id: ID of the code to execute. See the source of :class:`JobQueue.enqueue` for details.
:param list args: List of arguments to pass to the worker function
"""
self.id = random.getrandbits(32)
self.target_id = target_id
self.args = args
# The return and exception values are populated by the Worker later on
self.ret = None
self.exception = None
class Worker:
def __init__(self, host="localhost", port=6379, db=10):
"""
The workhorse of our implementation. Each worker fetches a job from Redis,
executes it, then stores the results back into Redis.
:param str host: Redis hostname
:param int port: Redis port
:param int db: Redis database number
"""
self.db = redis.Redis(host=host, port=port, db=db)
self.target_cache = {}
def _fetch_job(self):
return _loads(self.db.blpop("job_queue")[1])
def _do_job(self, target, job):
try:
args = job.args
if not isinstance(args, list) and not(isinstance(args, tuple)):
# Make sure that args are always a list/tuple
args = [args]
job.ret = target(*args)
except Exception as e:
# An exception occured, print and log it
traceback.print_exc()
job.exception = e
# Aadd the job to the response queue
self.db.rpush("response_queue", _dumps(job))
def run(self):
"""
In an infinite loop, wait for jobs, then execute them and return the results to Redis.
"""
while 1:
# Blocks until a job is available
job = self._fetch_job()
if job.target_id in self.target_cache:
# We have the target code cached, great!
target = self.target_cache[job.target_id]
else:
# Fetch the code from redis and cache it
target = _loads(self.db.get("target_%d" % (job.target_id)))
self.target_cache[job.target_id] = target
print("Got job: 0x%08x" % (job.id))
# Execute the job in a separate process
p = multiprocessing.Process(target=self._do_job, args=(target, job))
p.daemon = True
p.start()
p.join()
class JobQueue(Stream):
def __init__(self, host="localhost", port=6379, db=10):
"""
.. warning:: The :class:`JobQueue` flushes the selected Redis database! Be sure to specify an unused database!
The queue that allows submission and fetching of completed jobs.
:param str host: Redis hostname
:param int port: Redis port
:param int db: Redis database number
That being said, here is an example of how to use the queue.
>>> def sum(x, y):
... return x + y
>>> q = JobQueue()
>>> q.enqueue(sum, (1, 2)) # doctest: +SKIP
>>> q.enqueue(sum, (2, 3)) # doctest: +SKIP
>>> q.enqueue(sum, (3, 4)) # doctest: +SKIP
>>> q.finalize()
>>> for r in q: # doctest: +SKIP
... print r.ret
3
5
7
"""
Stream.__init__(self)
self.db = redis.Redis(host=host, port=port, db=db)
self.db.flushdb()
self.cnt_queued = 0
self.cnt_got = 0
self.finalized = False
def next(self):
if self.finalized and self.cnt_got == self.cnt_queued:
raise StopIteration
job = _loads(self.db.blpop("response_queue")[1])
self.cnt_got += 1
return job
def enqueue(self, target, args):
"""
Add a job to the queue.
:param function target: Function to be executed
:param list args: Arguments provided to the job
"""
if self.finalized:
raise Exception("No more jobs allowed")
# Check if we have to add the target's code to redis
target_data = _dumps(target)
target_id = hash(target_data)
if not self.db.get("target_%d" % (target_id)):
# This target does not exist, add it to the cache so that we don't have to download
# the method code every time a job is submitted/fetched.
self.db.set("target_%d" % (target_id), target_data)
# Add the new job to the redis list
self.db.rpush("job_queue", _dumps(Job(target_id, args)))
self.cnt_queued += 1
def finalize(self):
"""
Indicate to the queue that no more jobs will be submitted.
"""
self.finalized = True
class DistributedPool(Stream):
def __init__(self, function, host="localhost", port=6379, db=10):
"""
The distributed pool is a simple wrapper around the :class:`JobQueue` that makes is even more
convenient to use, just like :class:`ProcessPool` and :class:`ThreadPool`.
:param str host: Redis hostname
:param int port: Redis port
:param int db: Redis database number
First, on one machine let's start a single worker.
.. code-block:: bash
python streampie.py
We then execute:
>>> def mul(x, y):
... return x * y
>>> range(4) >> chop(2) >> DistributedPool(mul) >> list # doctest: +SKIP
[0, 6]
"""
Stream.__init__(self)
self.in_queue = queue.Queue()
self.out_queue = queue.Queue()
self.function = function
self.jq = JobQueue(host=host, port=port, db=db)
def _input_control(self):
# Move all data from the iterator to the input queue
for val in self.iterator:
self.jq.enqueue(self.function, val)
# Indicate to the queue that no more jobs will be added
self.jq.finalize()
def _output_control(self):
# Move all data from the job queue to the output queue
for job in self.jq:
self.out_queue.put(job.ret)
# All workers finished, stop the output queue iterator
self.out_queue.put(StopIteration)
def __iter__(self):
return _iterqueue(self.out_queue)
def _on_connect(self):
# Start the input and output control threads
self._input_thread = threading.Thread(target=self._input_control)
self._input_thread.daemon = True
self._input_thread .start()
self._output_control()
def stop(self):
"""
Currently not implemented. Is it even needed?
"""
return NotImplemented
if __name__ == "__main__":
# Act as a simple worker process
print("Starting worker...")
w = Worker()
w.run()
w.stop()