Asynchronous networking based on SimPy.
Project description
mosaik-simpy-io
mosaik-simpy-io is a fork of simpy.io to fix some problems for the use of simpy.io together with mosaik.
Version: 0.2.4
License: MIT
simpy.io
simpy.io is an asynchronous networking library based on SimPy. The nature of networking is thoroughly event-based (messages can for example be thought of as events). simpy.io marries the solid event system of SimPy with asynchronous input and output.
It provides several degrees of abstraction (raw sockets, packets and request-reply messages), supports various back-ends ((e)poll, select, asyncore, virtual) and lets you use different socket types, like TCP, SSL-encrypted, simulated). Furthermore protocols like HTTP, WebSockets and an extensible RPC interface are also supported.
Current status
simpy.io is currently in the early alpha phase of development. There is no guarantee for API stability (modules will almost certainly be renamed before the 1.0 release) and simpy.io may break on your system. Sadly, there isn’t any documentation available as of now (apart from the examples and tests).
The state of the individual modules is as follows:
sockets: stable alpha
packets: stable alpha
message: stable alpha
http: draft
websockets: draft
rpc: draft
Installation
simpy.io requires Python between 3.5 and 3.9 and SimPy 3. You can install it from via pip:
$ pip install mosaik-simpy-io
Examples
The following three examples demonstrate simpy.io’s levels of abstraction:
Socket level
When working directly with simpy.io sockets, you can try to read and write a specified number of bytes from or to a socket (note that there is no guarantee from the OS that all data will be received or transmitted):
>>> from simpy.io import select as backend
>>>
>>> def server(env, addr):
... server_sock = backend.TCPSocket.server(env, addr)
... sock = yield server_sock.accept()
... data = yield sock.read(4)
... print(data.decode())
... yield sock.write('cya'.encode())
>>>
>>> def client(env, addr):
... sock = backend.TCPSocket.connection(env, addr)
... yield sock.write('ohai'.encode())
... data = yield sock.read(3)
... print(data.decode())
>>>
>>> addr = ('127.0.0.1', 5555)
>>> env = backend.Environment()
>>> srv = env.process(server(env, addr))
>>> cli = env.process(client(env, addr))
>>> env.run(until=cli)
ohai
cya
Packet level
simpy.io packets alleviate the limitation of raw sockets and allow you to read and write complete packets. These can either be bytes (if you use Packet) or (unicode) strings (if you use PacketUTF8):
>>> from simpy.io import select as backend
>>> from simpy.io.packet import Packet
>>>
>>> def server(env, addr):
... server_sock = backend.TCPSocket.server(env, addr)
... sock = yield server_sock.accept()
... packet = Packet(sock)
... data = yield packet.read()
... print(data.decode())
... yield packet.write('cya'.encode())
>>>
>>> def client(env, addr):
... packet = Packet(backend.TCPSocket.connection(env, addr))
... yield packet.write('ohai'.encode())
... data = yield packet.read()
... print(data.decode())
>>>
>>> addr = ('127.0.0.1', 5556)
>>> env = backend.Environment()
>>> srv = env.process(server(env, addr))
>>> cli = env.process(client(env, addr))
>>> env.run(until=cli)
ohai
cya
Message level
The message level adds message counters that allow you to asynchronously send messages (even concurrently) and maps replies to their proper requests. Furthermore, you can specify (de)serializers (by default, JSON is used) and replies can signal success/failure:
>>> from simpy.io import select as backend
>>> from simpy.io.packet import PacketUTF8
>>> from simpy.io.message import Message
>>>
>>> def server(env, addr):
... server_sock = backend.TCPSocket.server(env, addr)
... sock = yield server_sock.accept()
... message = Message(env, PacketUTF8(sock))
... request = yield message.recv()
... print(request.content)
... yield request.succeed('cya')
>>>
>>> def client(env, addr):
... message = Message(env, PacketUTF8(
... backend.TCPSocket.connection(env, addr)))
... reply = yield message.send('ohai')
... print(reply)
>>>
>>> addr = ('127.0.0.1', 5557)
>>> env = backend.Environment()
>>> srv = env.process(server(env, addr))
>>> cli = env.process(client(env, addr))
>>> env.run(until=cli)
ohai
cya
Help & Contact
Bugs should be posted on our issue tracker here on GitLab.
Changelog
0.2.4 - 2022-05-12
[FIX] Fix for external event simulations in realtime
[FIX] Renamed async.py to asnycio.py to fix problem with import of async, which is a keyword since Python 3.5
[FIX] Constrain simpy version to <4.0.0 due to incompatibility
[CHANGE] python 3.5, 3.6, 3.7, 3.8 and 3.9 are currently supported, python <=3.4 not anymore.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mosaik-simpy-io-0.2.4.tar.gz
.
File metadata
- Download URL: mosaik-simpy-io-0.2.4.tar.gz
- Upload date:
- Size: 48.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b10d6e81ec3e0f87f95b46b16ce4ebcad195963c1a75d182aedfcf67ac0e20a |
|
MD5 | c7a204c526e9b8305dbb780983675505 |
|
BLAKE2b-256 | 7142e924ee0d13accdd4f2cdb8145280afebea8c7794c63d5d420c983e9a8143 |
Provenance
File details
Details for the file mosaik_simpy_io-0.2.4-py2.py3-none-any.whl
.
File metadata
- Download URL: mosaik_simpy_io-0.2.4-py2.py3-none-any.whl
- Upload date:
- Size: 71.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a55cf5057ef85f764f948d0a1b1cb2a6224e626952dd7500bb42bba98cf3e41 |
|
MD5 | 19175659d5bed4f7f9dc19af44a368d6 |
|
BLAKE2b-256 | 617144206b2e94b2fdf15792dedc95a3662fee6858b07a8d2024d683a8159b1d |