-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample_multiprocessing_simple_process_y.py
53 lines (42 loc) · 1.68 KB
/
example_multiprocessing_simple_process_y.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
from stream import Stream, ExternalStream
from example_operators import single_item
class Count(object):
"""
Persistent integer used in callbacks.
"""
def __init__(self, n): self.n = n
def process_target_y(dict_queues):
#-------------------------------------------
# 1. SPECIFY INPUT QUEUES FOR THE PROCESSES
#-------------------------------------------
Stream.scheduler.input_queue = dict_queues['y']
#-------------------------------------------
# 2. SPECIFY STREAMS IN THIS PROCESS
#-------------------------------------------
y = Stream(name='y')
#-------------------------------------------
# 3. SPECIFY EXTERNAL STREAMS
#-------------------------------------------
x = ExternalStream(name='x', queue=dict_queues['x'])
#-------------------------------------------
# 4. SPECIFY CALLBACK FUNCTIONS IN THIS PROCESS
#-------------------------------------------
count = Count(3)
def callback_y(stream_item, count):
if not count.n > 0:
x.append('__halt__')
Stream.scheduler.halted = True
return
print('message received by process y: ', stream_item)
x.append(stream_item+1)
count.n -= 1
#-------------------------------------------
# 5. CREATE AGENTS IN THIS PROCESS
#-------------------------------------------
single_item(in_stream=y, func=callback_y, count = count)
# Initiate computation by sending 1 on external stream x
x.append(1)
#-------------------------------------------
# 6. START SCHEDULER
#-------------------------------------------
Stream.scheduler.start()