Skip to content

Commit 06aca6c

Browse files
committed
Implement first part of library (Network and Process)
1 parent d86b0b6 commit 06aca6c

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

examples/string_processing_pipeline.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import asyncio
2-
2+
import flowbase
33

44
def main():
5-
loop = asyncio.get_event_loop()
5+
net = flowbase.Network()
66

77
# Initialize components
88
hisayer = HiSayer()
9-
loop.create_task(hisayer.run())
9+
net.add_process('hisayer', hisayer)
1010

1111
splitter = StringSplitter()
12-
loop.create_task(splitter.run())
12+
net.add_process('hisayer', splitter)
1313

1414
lowercaser = LowerCaser()
15-
loop.create_task(lowercaser.run())
15+
net.add_process('hisayer', lowercaser)
1616

1717
uppercaser = UpperCaser()
18-
loop.create_task(uppercaser.run())
18+
net.add_process('hisayer', uppercaser)
1919

2020
stringjoiner = StringJoiner()
21-
loop.create_task(stringjoiner.run())
21+
net.add_process('hisayer', stringjoiner)
2222

2323
printer = Printer()
24-
loop.create_task(printer.run())
24+
net.add_process('hisayer', printer)
2525

2626
# Connect network
2727
splitter.in_lines = hisayer.out_lines
@@ -32,7 +32,7 @@ def main():
3232
printer.in_lines = stringjoiner.out_lines
3333

3434
# Run the full event loop
35-
loop.run_until_complete(printer.run())
35+
net.run()
3636

3737

3838
class HiSayer:

flowbase/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from flowbase.flowbase import Process, Network

flowbase/flowbase.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Copyright (c) 2020 Samuel Lampa <samuel.lampa@rilnet.com>
3+
'''
4+
5+
import asyncio
6+
import typing
7+
8+
class Process:
9+
def run(self):
10+
raise NotImplementedError(f'str(type(self)) can not be used directly, but must be subclassed')
11+
12+
13+
class Network(Process):
14+
_processes = {}
15+
_driver_process = None
16+
17+
def __init__(self):
18+
self._loop = asyncio.get_event_loop()
19+
20+
def add_process(self, name: str, process: Process):
21+
self._processes[name] = process
22+
self._loop.create_task(process.run())
23+
self._driver_process = process
24+
25+
def run(self):
26+
self._loop.run_until_complete(self._driver_process.run())

0 commit comments

Comments
 (0)