Package: data-place
data-placeA powerful and flexible framework for designing async socket based data streaming and distribution systems, with automated parsing, dynamic data store and high-level control hooks.Installationpip install data-placeexampleintegrates with dataclasses and pydantic as a side effectA definition of a data model.
Can be either shared globally or reimplemented separately.
Also defines an async loop function that will be used to produce Data objects.fromdataclassesimportdataclassimportasyncioimportrandomfromuuidimportuuid4fromdataplaceimportModelIO,Controller@dataclass(slots=True,frozen=True)classData(ModelIO):id:strvalue:intasyncdefproduce(controller:Controller)->None:whilecontroller.running:awaitcontroller.async_hold()awaitcontroller.async_callback(Data(id=str(uuid4()),value=random.randint(0,9)))awaitasyncio.sleep(1)async socket based data sending serverimportasynciofromdataplaceimportSender,Controller,Callback,SpaceStorestore=SpaceStore[int,Data](lambdadata:data.value,Data)server=Sender.Socket.Server(host="127.0.0.1",port=5555)controller=Controller(callbacks=[Callback(store.add,types={Data}),Callback(server.call,types={Data}),Callback(print,types={Data})])loop=asyncio.new_event_loop()loop.create_task(produce(controller))loop.create_task(server.start())loop.run_forever()async socket based data receiving clientimportasynciofromdataplaceimportReceiver,Callback,SpaceStorestore=SpaceStore[int,Data](lambdadata:data.value,Data)client=Receiver.Socket.Client(host="127.0.0.1",port=5555,callbacks=[Callback(store.add,types={Data}),Callback(print,types={Data})])loop=asyncio.new_event_loop()loop.create_task(client.start())loop.run_forever()async websocket based data sending serverimportasynciofromdataplaceimportSender,Controller,Callback,SpaceStorestore=SpaceStore[int,Data](lambdadata:data.value,Data)server=Sender.WebSocket.Server(host="127.0.0.1",port=5555)controller=Controller(callbacks=[Callback(store.add,types={Data}),Callback(server.call,types={Data}),Callback(print,types={Data})])loop=asyncio.new_event_loop()loop.create_task(produce(controller))loop.create_task(server.start())loop.run_forever()async websocket based data receiving clientimportasynciofromdataplaceimportReceiver,Callback,SpaceStorestore=SpaceStore[int,Data](lambdadata:data.value,Data)client=Receiver.WebSocket.Client(url="ws://127.0.0.1:5555",callbacks=[Callback(store.add,types={Data}),Callback(print,types={Data})])loop=asyncio.new_event_loop()loop.create_task(client.start())loop.run_forever()async socket based data receiving serverimportasynciofromdataplaceimportReceiver,Callback,SpaceStorestore=SpaceStore[int,Data](lambdadata:data.value,Data)server=Receiver.Socket.Server(host="127.0.0.1",port=5555,callbacks=[Callback(store.add,types={Data}),Callback(print,types={Data})])loop=asyncio.new_event_loop()loop.create_task(server.start())loop.run_forever()async socket based data sending clientstore=SpaceStore[int,Data](lambdadata:data.value,Data)client=Sender.Socket.Client(host="127.0.0.1",port=5555)controller=Controller(callbacks=[Callback(store.add,types={Data}),Callback(client.call,types={Data}),Callback(print,types={Data})])loop=asyncio.new_event_loop()loop.run_until_complete(client.start())loop.create_task(produce(controller))loop.run_forever()async websocket based data receiving serverimportasynciofromdataplaceimportReceiver,Callback,SpaceStorestore=SpaceStore[int,Data](lambdadata:data.value,Data)server=Receiver.WebSocket.Server(host="127.0.0.1",port=5555,callbacks=[Callback(store.add,types={Data}),Callback(print,types={Data})])loop=asyncio.new_event_loop()loop.create_task(server.start())loop.run_forever()async websocket based data sending clientimportasynciofromdataplaceimportSender,Callback,SpaceStorestore=SpaceStore[int,Data](lambdadata:data.value,Data)client=Sender.WebSocket.Client(url="ws://127.0.0.1:5555")controller=Controller(callbacks=[Callback(store.add,types={Data}),Callback(client.call,types={Data}),Callback(print,types={Data})])loop=asyncio.new_event_loop()loop.run_until_complete(client.start())loop.create_task(produce(controller))loop.run_forever()