-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_send.py
83 lines (64 loc) · 2.36 KB
/
json_send.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
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
# pyplexo
# Copyright © 2018-2023 Alecks Gates
#
# pyplexo is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyplexo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyplexo. If not, see <https://www.gnu.org/licenses/>.
import asyncio
import logging
from timeit import default_timer as timer
import python_jsonschema_objects as pjs
from plexo.axon import Axon
from plexo.codec.json_codec import JsonCodec
from plexo.neuron.neuron import Neuron
from plexo.exceptions import TransmitterNotFound
from plexo.namespace.namespace import Namespace
from plexo.plexus import Plexus
foo_schema = {
"title": "Foo",
"type": "object",
"properties": {
"message": {"type": "string"},
},
"required": ["message"],
}
builder = pjs.ObjectBuilder(foo_schema)
jsonschema_namespace = builder.build_classes()
Foo = jsonschema_namespace[foo_schema["title"]]
async def _foo_reaction(f: Foo, _, _2): # type: ignore
logging.info(f"Received Foo.message: {f.message}") # type: ignore
async def send_foo_hello_str(axon: Axon):
i = 1
foo = Foo()
while True:
start_time = timer()
foo.message = f"Hello, Plexo+Inproc {i} …"
logging.info(f"Sending Foo with message: {foo.message}")
logging.debug(f"JSON: {foo.serialize()}")
try:
await axon.transmit(foo)
except TransmitterNotFound as e:
logging.error(e)
i += 1
await asyncio.sleep(1 - (start_time - timer()))
def run():
logging.basicConfig(level=logging.DEBUG)
foo_codec = JsonCodec(Foo)
plexus = Plexus()
namespace = Namespace(["dev", "plexo", "test"])
foo_neuron = Neuron(foo_codec.schema_class, namespace, foo_codec)
foo_plexus_axon = Axon(foo_neuron, plexus)
asyncio.run(foo_plexus_axon.react(reactants=[_foo_reaction]))
asyncio.run(send_foo_hello_str(foo_plexus_axon))
plexus.close()
if __name__ == "__main__":
run()