-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstop_on_error_pyreactor.py
53 lines (38 loc) · 1.19 KB
/
stop_on_error_pyreactor.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import sys
import time
import os.path
sys.path.append(os.path.join(os.getcwd(), '.'))
sys.path.append(os.path.join(os.getcwd(), '..'))
from pyreactor.reactor import Reactor
logger = logging.getLogger()
handler = logging.NullHandler()
logger.addHandler(handler)
def add_5(x):
"""
Sample action. Add 5 to a number.
:param x: the task
:type x: int or float
:return: 5 more than number
:rtype: int or float
"""
_sleep_secs = 5
log_msg = 'consumer pid {} - '.format(os.getpid())
log_msg += 'Adding 5 to {}; after sleeping for {} secs'.format(x,
_sleep_secs)
logger.warn(log_msg)
time.sleep(_sleep_secs)
return x + 5
def main():
# five tasks
_tasks = ['1', 2, 3, 4, 5]
# no-stop-on-error reactor; continue processing even if one consumer fails
# result_timeout: max time taken to complete 1 task
_reactor = Reactor(stop_on_error=True, parallelism=2,
result_timeout=10)
_results = _reactor.run(action=add_5, tasks=_tasks)
print _results
if __name__ == '__main__':
main()