zerorpc-python

A flexible RPC based on zeromq
Download

zerorpc-python Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • DotCloud Inc.
  • Publisher web site:
  • http://www.dotcloud.com

zerorpc-python Tags


zerorpc-python Description

zerorpc-python is a flexible RPC implementation based on zeromq and messagepack. Service APIs exposed with zerorpc are called "zeroservices".zerorpc can be used programmatically or from the command-line. It comes with a convenient script, "zerorpc", allowing to:- expose Python modules without modifying a single line of code,- call those modules remotely through the command line.Create a server with a one-linerLet's see zerorpc in action with a simple example. In a first terminal, we will expose the Python "time" module: zerorpc --server --bind tcp://*:1234 timeNoteThe bind address uses the zeromq address format. You are not limited to TCP transport: you could as well specify ipc:///tmp/time to use host-local sockets, for instance. "tcp://*:1234" is a short-hand to "tcp://0.0.0.0:1234" and means "listen on TCP port 1234, accepting connections on all IP addresses".Call the server from the command-lineNow, in another terminal, call the exposed module:$ zerorpc --client --connect tcp://*:1234 strftime %Y/%m/%dConnecting to "tcp://*:1234""2011/03/07"Since the client usecase is the most common one, "--client" is the default parameter, and you can remove it safely:$ zerorpc --connect tcp://*:1234 strftime %Y/%m/%dConnecting to "tcp://*:1234""2011/03/07"Moreover, since the most common usecase is to connect (as opposed to bind) you can also omit "--connect":$ zerorpc tcp://*:1234 strftime %Y/%m/%dConnecting to "tcp://*:1234""2011/03/07"See remote service documentationYou can introspect the remote service; it happens automatically if you don't specify the name of the function you want to call:$ zerorpc tcp://*:1234Connecting to "tcp://*:1234"tzset tzset(zone)ctime ctime(seconds) -> stringclock clock() -> floating point numberstruct_time time time() -> floating point numberstrptime strptime(string, format) -> struct_timegmtime gmtime() -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,mktime mktime(tuple) -> floating point numbersleep sleep(seconds)asctime asctime() -> stringstrftime strftime(format) -> stringlocaltime localtime() -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,Specifying non-string argumentsNow, see what happens if we try to call a function expecting a non-string argument:$ zerorpc tcp://*:1234 sleep 3Connecting to "tcp://*:1234"Traceback (most recent call last):TypeError: a float is requiredThat's because all command-line arguments are handled as strings. Don't worry, we can specify any kind of argument using JSON encoding:$ zerorpc --json tcp://*:1234 sleep 3Connecting to "tcp://*:1234"nullzeroworkers: reversing bind and connectSometimes, you don't want your client to connect to the server; you want your server to act as a kind of worker, and connect to a hub or queue which will dispatch requests. You can achieve this by swapping "--bind" and "--connect": zerorpc --bind tcp://*:1234 localtimeWe now have "something" wanting to call the "localtime" function, and waiting for a worker to connect to it. Let's start the worker: zerorpc --server tcp://*:1234 timeThe worker will connect to the listening client and ask him "what should I do?"; the client will send the "localtime" function call; the worker will execute it and return the result. The first program will display the local time and exit. The worker will remain running.Listening on multiple addressesWhat if you want to run the same server on multiple addresses? Just repeat the "--bind" option: zerorpc --server --bind tcp://*:1234 --bind ipc:///tmp/time timeYou can then connect to it using either "zerorpc tcp://*:1234" or "zerorpc ipc:///tmp/time".Wait, there is more! You can even mix "--bind" and "--connect". That means that your server will wait for requests on a given address, and connect as a worker on another. Likewise, you can specify "--connect" multiple times, so your worker will connect to multiple queues. If a queue is not running, it won't affect the worker (that's the magic of zeromq).WarningA client should probably not connect to multiple addresses!Almost all other scenarios will work; but if you ask a client to connect to multiple addresses, and at least one of them has no server at the end, the client will ultimately block. A client can, however, bind multiple addresses, and will dispatch requests to available workers. If you want to connect to multiple remote servers for high availability purposes, you insert something like HAProxy in the middle.Exposing a zeroservice programmaticallyOf course, the command-line is simply a convenience wrapper for the zerorpc python API. Below are a few examples.Here's how to expose an object of your choice as a zeroservice:class Cooler: """ Various convenience methods to make things cooler. """ def add_man(self, sentence): """ End a sentence with ", man!" to make it sound cooler, and return the result. """ return sentence + ", man!" def add_42(self, n): """ Add 42 to an integer argument to make it cooler, and return the result. """ return n + 42 def boat(self, sentence): """ Replace a sentence with "I'm on a boat!", and return that, because it's cooler. """ return "I'm on a boat!"import zerorpcs = zerorpc.Server(Cooler())s.bind("tcp://0.0.0.0:4242")s.run()Let's save this code to cooler.py and run it:python cooler.pyNow, in another terminal, let's try connecting to our awesome zeroservice:$ zerorpc -j tcp://:4242 add_42 143$ zerorpc tcp://:4242 add_man 'I own a mint-condition Wolkswagen Golf'"I own a mint-condition Wolkswagen Gold, man!"$ zerorpc tcp://:4242 boat 'I own a mint-condition Wolkswagen Gold, man!'"I'm on a boat!"Congratulations! You have just made the World a little cooler with your first zeroservice, man!Product's homepage


zerorpc-python Related Software