Skip to content

Commit

Permalink
Added zeromq examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbr committed Feb 1, 2013
1 parent 17b8c40 commit 5aacee9
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,64 @@ These can be answered by a server implemented as follows:
rpc_server.serve_forever()
0mq
~~~

An example using :py:mod:`zmq` is very similiar, differing only in the
instantiation of the transport:

.. code-block:: python
import zmq
from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.zmq import ZmqClientTransport
from tinyrpc import RPCClient
ctx = zmq.Context()
rpc_client = RPCClient(
JSONRPCProtocol(),
ZmqClientTransport.create(ctx, 'tcp://127.0.0.1:5001')
)
remote_server = rpc_client.get_proxy()
# call a method called 'reverse_string' with a single string argument
result = remote_server.reverse_string('Hello, World!')
print "Server answered:", result
Matching server:

.. code-block:: python
import zmq
from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.zmq import ZmqServerTransport
from tinyrpc.server import RPCServer
from tinyrpc.dispatch import RPCDispatcher
ctx = zmq.Context()
dispatcher = RPCDispatcher()
transport = ZmqServerTransport.create(ctx, 'tcp://127.0.0.1:5001')
rpc_server = RPCServer(
transport,
JSONRPCProtocol(),
dispatcher
)
@dispatcher.public
def reverse_string(s):
return s[::-1]
rpc_server.serve_forever()
Further examples
----------------

Expand Down
22 changes: 22 additions & 0 deletions examples/zmq_client_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import zmq

from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.zmq import ZmqClientTransport
from tinyrpc import RPCClient

ctx = zmq.Context()

rpc_client = RPCClient(
JSONRPCProtocol(),
ZmqClientTransport.create(ctx, 'tcp://127.0.0.1:5001')
)

remote_server = rpc_client.get_proxy()

# call a method called 'reverse_string' with a single string argument
result = remote_server.reverse_string('Hello, World!')

print "Server answered:", result
25 changes: 25 additions & 0 deletions examples/zmq_server_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import zmq

from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.zmq import ZmqServerTransport
from tinyrpc.server import RPCServer
from tinyrpc.dispatch import RPCDispatcher

ctx = zmq.Context()
dispatcher = RPCDispatcher()
transport = ZmqServerTransport.create(ctx, 'tcp://127.0.0.1:5001')

rpc_server = RPCServer(
transport,
JSONRPCProtocol(),
dispatcher
)

@dispatcher.public
def reverse_string(s):
return s[::-1]

rpc_server.serve_forever()

0 comments on commit 5aacee9

Please sign in to comment.