Foundations of Python Network Programming

(WallPaper) #1
331

Chapter 18

RPC


Remote Procedure Call (RPC) systems let you call a function in another process or on a remote server using the same
syntax you would use when calling a routine in a local API or library. This tends to be useful in two situations:


•    Your program has a lot of work to do, and you want to spread it across several machines by
making calls across the network, but without having to change the code that is making the call,
which is now remote.

•    You need data or information that is only available on another hard drive or network, and an
RPC interface lets you easily send queries to another system to get back an answer.

The first remote procedure systems tended to be written for low-level languages like C. They placed bytes on the
network that looked very much like the bytes already being written onto the processor stack every time one C function
called another. And just as a C program could not safely call a library function without a header file that told it exactly
how to lay out the function’s arguments in memory (any errors often resulted in a crash), RPC calls could not be made
without knowing ahead of time how the data would be serialized. In fact, each RPC payload looked exactly like a block
of binary data as formatted by the Python struct module discussed in Chapter 5.
Today our machines and networks are fast enough, though, so we often exchange some memory and speed for
protocols that are more robust and require less coordination between two pieces of code that are in conversation.
Older RPC protocols would have sent a stream of bytes like the following:


0, 0, 0, 1, 64, 36, 0, 0, 0, 0, 0, 0


It would have been up to the receiver to know that the function’s parameters are a 32-bit integer and a 64-bit
floating point number, and then to decode the 12 bytes to the pair of values “integer 1” and “float 10.0.” More modern
RPC protocols use self-documenting formats like XML, however, which are written in a way that makes it all but
impossible to interpret the arguments as anything other than an integer and a floating-point number:



41
10.

An earlier generation of programmer would have been appalled that 12 bytes of actual binary data have bloated
into 108 bytes of protocol that have to be generated by the sender and then parsed on the receiving end, consuming
hundreds of CPU (Central Processing Unit) cycles. Nonetheless, the elimination of ambiguity in the protocols is
generally considered worth the expense. Of course, the above pair of values can also be expressed with less verbosity
by using a more modern payload format than XML, like JSON the JavaScript Object Notation:


[1, 10.0]

Free download pdf