Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
229 lines (180 loc) · 6.08 KB
/
Copy path__init__.py
File metadata and controls
229 lines (180 loc) · 6.08 KB
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
"""Coroutine building block functions
Based on David Beazley's coroutine code from http://dabeaz.com/coroutines/
==========
decorators
==========
- `coroutine(func)`, Calls .next() on func to init func as a coroutine
==================
control coroutines
==================
- `broadcast`, forwards what is sent to it to provided coroutines
- `threaded`, Parallelizes the pipeline with N threads
- `multiprocessed`, Parallelizes the pipeline with N processes
=====
sinks
=====
- `sinks.printer`, Prints whatever is sent to it
- `sinks.locking_printer`, Printer for threaded or multiprocessed
=======
utility
=======
- `utility.cat`, Sends lines from file to target
- `utility.grep`, Sends matching text lines to target
- `utility.dirwalk`, Sends filepaths to target
- `utility.co_filter`, Filters data using a function
"""
__docformat__ = 'restructuredtext'
__all__ = ['coroutine', 'broadcast', 'threaded', 'multiprocessed', 'sinks', 'utility']
import functools
def coroutine(func):
"""
Initializes func as a coroutine.
Parameters:
- `func`: a function to be decorated as a coroutine
"""
@functools.wraps(func)
def start(*args,**kwargs):
cr = func(*args,**kwargs)
cr.next()
return cr
return start
@coroutine
def broadcast(*targets):
"""
Coroutine. Forwards what is sent to it to each target in targets.
.send() accepts data to forward
Parameters:
- `targets`: a list of coroutines, next stage of pipeline
"""
while True:
try:
item = (yield)
for target in targets:
target.send(item)
except GeneratorExit:
for target in targets:
target.close()
return
import threading
import Queue
class threaded(object):
"""
Class acting as a coroutine. Creates specified number of threads
and passes data to them via a queue. Threads take data as soon as they
are free and it is available then pass it to their targets.
"""
def __init__(self, num_threads, wrapped_target):
"""
Spawn threads to send data to target.
Parameters:
- `num_threads`: an integer, number of threads to spawn
- `wrapped_target`: coroutine wrapped in function, next stage of
pipeline
example::
# using the coroutines grep and printer, create a wrapped coroutine
wrapped_co = lambda : grep(r'important', printer())
# create the rest of the pipeline
# it will cat some file into 4 threads that will grep and print
pipeline = cat(threaded(4, wrapped_co))
pipeline.send('textfile.txt')
"""
self._stopEvent = threading.Event()
self._wrapped_target = wrapped_target
self._num_threads = num_threads
self.threaded = self._threaded()
@coroutine
def _threaded(self):
messages = Queue.Queue()
def run_target(stopEvent):
target = self._wrapped_target()
while not stopEvent.is_set():
item = messages.get()
if item is GeneratorExit:
break
else:
target.send(item)
target.close()
for i in xrange(self._num_threads):
threading.Thread(target=run_target, args=(self._stopEvent, )).start()
try:
while True:
item = (yield)
messages.put(item)
except GeneratorExit:
for i in xrange(self._num_threads):
messages.put(GeneratorExit)
def stop(self):
"""Signal the threads to stop."""
self._stopEvent.set()
def send(self, arg):
"""
Simulates coroutine .send() method. Accepts data to forward to
target coroutines.
Parameters:
- `arg`: an object, data to be forwarded
"""
self.threaded.send(arg)
def close(self):
"""
Simulates coroutine .close() method. Closes this coroutine and
signals target coroutine to close().
"""
self.threaded.close()
import multiprocessing
class multiprocessed(object):
"""
Class acting as a coroutine that passes data on to its target via a
queue and a multiprocessing.Process().
"""
def __init__(self, num_processes, target):
"""
Spawn processes to send data to target.
Parameters:
- `num_processes`: an integer, number of processes to spawn
- `target`: a coroutine, next stage of pipeline
"""
self._stopEvent = multiprocessing.Event()
self._target = target
self._num_processes = num_processes
self.multiprocessed = self._multiprocessed()
@coroutine
def _multiprocessed(self):
target = self._target
messages = multiprocessing.Queue()
def run_target(stopEvent):
while not stopEvent.is_set():
item = messages.get()
if item is GeneratorExit:
break
else:
target.send(item)
target.close()
for i in xrange(self._num_processes):
multiprocessing.Process(
target=run_target, args=(self._stopEvent, )).start()
try:
while True:
item = (yield)
messages.put(item)
except GeneratorExit:
for i in xrange(self._num_processes):
messages.put(GeneratorExit)
def stop(self):
"""Signal the processes to stop."""
self._stopEvent.set()
def send(self, arg):
"""
Simulates coroutine .send() method. Accepts data to forward to
target coroutines.
Parameters:
- `arg`: an object, data to be forwarded
"""
self.multiprocessed.send(arg)
def close(self):
"""
Simulates coroutine .close() method. Closes this coroutine and
signals target coroutine to close().
"""
self.multiprocessed.close()
import sinks
import utility