-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add result buffering/batch processing example.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
Process 20 lines of output at a time. | ||
Example runs: | ||
python buffer_results.py | ||
""" | ||
import sys | ||
import time | ||
import drainers | ||
|
||
# fake this | ||
def setup_cruncher(): | ||
time.sleep(1) | ||
|
||
def do_something_expensive(file): | ||
time.sleep(0.005) | ||
|
||
def destroy_cruncher(): | ||
time.sleep(0.8) | ||
|
||
files = [] | ||
|
||
def crunch(files): | ||
print 'Setting up cruncher...' | ||
setup_cruncher() | ||
while len(files) > 0: | ||
f = files.pop(0) | ||
print '- Crunching file %s...' % f.strip() | ||
do_something_expensive(f) | ||
print 'Releasing cruncher...' | ||
destroy_cruncher() | ||
|
||
def add_to_buffer(line, is_err): | ||
if is_err: | ||
# ignore all errors | ||
return | ||
files.append(line) | ||
|
||
# start crunch synchronously after 20 items have been read | ||
if len(files) >= 20: | ||
crunch(files) | ||
|
||
d = drainers.Drainer(['find', '.', '-type', 'f'], read_event_cb=add_to_buffer) | ||
d.start() |