Package: simpy.io
simpy.iois an asynchronous networking library based onSimPy. The nature
of networking is thoroughly event-based (messages can for example be thought of
as events).simpy.iomarries the solid event system ofSimPywith
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 statussimpy.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: stablealphapackets: stablealphamessage: stablealphahttp:draftwebsockets:draftrpc:draftThe following table represents the support matrix. As of now, there’s no CI
system in place to guarantee the test stability, yet.SystemFlavorPythonBackendsSuccessful testsLinuxArchLinux2.6.9, 2.7.6,
3.3, 3.4allallLinuxUbuntu (12.04)2.6.9, 2.7.6,
3.3allmostWindowsWindows 72.6.9, 2.7.6,
3.3selectallOS XMaverick (10.9)3.3noepollmostNotePython 2 does not natively support all of SimPy’s features (e.g. no
exception chains or return statements inside of generators) but workarounds
are available.Installationsimpy.io requires Python 2.7 or 3.3 and SimPy 3. You can install it from
Bitbucket via pip:$pipinstallhg+https://bitbucket.org/simpy/simpy.ioExamplesThe following three examples demonstrate simpy.io’s levels of abstraction:Socket levelWhen working directly with simpy.io sockets, you can try toreadandwritea 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):>>>fromsimpy.ioimportselectasbackend>>>>>>defserver(env,addr):...server_sock=backend.TCPSocket.server(env,addr)...sock=yieldserver_sock.accept()...data=yieldsock.read(4)...print(data.decode())...yieldsock.write('cya'.encode())>>>>>>defclient(env,addr):...sock=backend.TCPSocket.connection(env,addr)...yieldsock.write('ohai'.encode())...data=yieldsock.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)ohaicyaPacket levelsimpy.io packets alleviate the limitation of raw sockets and allow you to read
and write complete packets. These can either be bytes (if you usePacket)
or (unicode) strings (if you usePacketUTF8):>>>fromsimpy.ioimportselectasbackend>>>fromsimpy.io.packetimportPacket>>>>>>defserver(env,addr):...server_sock=backend.TCPSocket.server(env,addr)...sock=yieldserver_sock.accept()...packet=Packet(sock)...data=yieldpacket.read()...print(data.decode())...yieldpacket.write('cya'.encode())>>>>>>defclient(env,addr):...packet=Packet(backend.TCPSocket.connection(env,addr))...yieldpacket.write('ohai'.encode())...data=yieldpacket.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)ohaicyaMessage levelThe 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,JSONis used) and
replies can signal success/failure:>>>fromsimpy.ioimportselectasbackend>>>fromsimpy.io.packetimportPacketUTF8>>>fromsimpy.io.messageimportMessage>>>>>>defserver(env,addr):...server_sock=backend.TCPSocket.server(env,addr)...sock=yieldserver_sock.accept()...message=Message(env,PacketUTF8(sock))...request=yieldmessage.recv()...print(request.content)...yieldrequest.succeed('cya')>>>>>>defclient(env,addr):...message=Message(env,PacketUTF8(...backend.TCPSocket.connection(env,addr)))...reply=yieldmessage.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)ohaicyaHelp & ContactPlease feel free to post a message to theSimPy-Users mailing listto ask
for help or to discuss the ongoing development. Bugs should be posted on ourissue trackerhere on BitBucket.