Overview

16 Scripting Apache Thrift

This chapter shows how Apache Thrift enables fast, cross-language RPC development in popular scripting languages—particularly those associated with the LAMP ecosystem. It explains installing the Thrift compiler and language libraries, generating client and server stubs from a minimal helloSvc service, and highlights how scripting languages are well suited for rapid prototyping and for building lightweight services that can evolve without breaking contracts. The discussion emphasizes broad language-version support, typical development flow (compile IDL, wire transports/protocols, implement handlers), and the importance of matching client/server I/O stacks.

Ruby serves as a complete example of building both clients and servers. After installing the thrift gem (optionally compiling a native C extension), the IDL compiler emits Ruby modules containing Client and Processor classes. The chapter walks through a simple server using TSocket, TBufferedTransport, and TBinaryProtocol, then demonstrates a matching client, and outlines alternative servers (single-threaded, threaded, thread pool, nonblocking, and HTTP). It summarizes key Ruby features: intuitive type mappings, namespace-to-module generation, several endpoint transports (sockets, HTTP, UNIX sockets, memory, IO streams), buffered/framed transports, and multiple protocols (binary, compact, JSON), including a performance-oriented accelerated binary protocol option.

For PHP, the focus is primarily on clients invoked from web apps: dependencies are managed with Composer, stubs are generated with the compiler, and a simple browser form is wired to call the helloSvc over a socket using the binary protocol. PHP can generate server stubs (typically HTTP-based), supports multiplexing, offers several transports (sockets, HTTP, memory, streams) and protocols (binary—with an accelerated variant—compact, JSON), and provides simple and forking servers, with a note that server performance is modest. Perl coverage includes installing the Thrift library via cpanm, generating stubs, and implementing a minimal client and a forking server. Perl supports sockets, UNIX sockets, HTTP, memory buffers, and the binary protocol, with optional performance enhancements and compact support via native extensions (e.g., Thrift::XS), plus multiplexing. Python installation via pip/pip3 is shown for both Python 2 and 3, with the six library smoothing compatibility; the same hello example runs across versions once the stubs and libraries are in place.

- PHP and Apache Thrift
- hello_test.php
-3 - PHP RPC round trip

  Summary

This chapter has covered:

  • Installing Apache Thrift for Ruby
  • Creating Ruby RPC clients
  • Creating Ruby RPC servers
  • Installing Apache Thrift for PHP
  • Creating RPC clients in PHP
  • Installing Apache Thrift for Perl
  • Creating RPC clients in Perl
  • Installing Apache Thrift for Python 2 and 3

FAQ

What does “Scripting Apache Thrift” cover and why use scripting languages with Thrift?It shows how to install, generate code for, and build Apache Thrift clients and servers in Ruby, PHP, Perl, and Python. These languages (common in the LAMP stack) are great for quick prototypes, testing/mocking services, and even microservices (notably Python and Ruby). With Thrift, you can swap implementations later without breaking the interface contract.
How do I build a simple Ruby Thrift client and server?- Install: gem install thrift (ruby-dev may be needed for native extensions)
- Generate stubs: thrift --gen rb hello.thrift (creates gen-rb/* files)
- Server basics: implement a handler (e.g., getMessage), wrap with HelloSvc::Processor, choose I/O stack (ServerSocket + BufferedTransportFactory + BinaryProtocolFactory), and run Thrift::SimpleServer on a port (e.g., 9095).
- Client basics: build the same stack (Socket + BufferedTransport + BinaryProtocol), create HelloSvc::Client, open, call getMessage, close.
- Ensure client and server use compatible transports/protocols.
Which Ruby files does the Thrift compiler generate and what’s inside them?- hello_constants.rb: all IDL constants (none in the hello example).
- hello_types.rb: user-defined types/structs/unions (none in the hello example).
- hello_svc.rb: service module (e.g., module HelloSvc) containing Client and Processor classes, plus internal argument/result classes for each method.
What transports, protocols, and servers are available in Ruby?- End-point transports: TSocket, HTTPClientTransport, UNIXSocket, MemoryBufferTransport, IOStreamTransport.
- Framing/buffering: use TBufferedTransport or TFramedTransport (framed required by some nonblocking servers).
- Protocols: Binary (Thrift::BinaryProtocol and BinaryProtocolAccelerated), Compact, JSON. The Accelerated variant is a C extension for faster Binary serialization (modest speedups, platform-dependent).
- Servers: SimpleServer (single-threaded), ThreadedServer, ThreadPoolServer, NonblockingServer, and HTTP servers (ThinHTTPServer, MongrelHTTPServer).
How do I set up a PHP Thrift client and why is PHP mainly client-focused?PHP typically runs inside a web server, so it’s more commonly used as a Thrift client calling backend services. The compiler generates client code by default; server stubs require --gen php:server and generally run over HTTP inside a web server. Setup steps:
- Install PHP and a web server (e.g., apache2 + libapache2-mod-php).
- Use Composer with composer.json requiring "apache/thrift". Run php composer.phar install to fetch libraries.
- Generate client stubs: thrift --gen php hello.thrift (produces gen-php/*).
- In your PHP code: require vendor/autoload.php and the generated service files; build TSocket/TBufferedTransport/TBinaryProtocol; instantiate the service client; open, call, close.
What transports, protocols, and servers does PHP support, and any caveats?- Transports: TSocket, THttpClient, TCurlClient, TMemoryBuffer, TPhpStream; buffering/framing layers are available.
- Protocols: TBinaryProtocol (and Accelerated variant), TCompactProtocol, TJSONProtocol.
- Servers: TSimpleServer (single-threaded), TForkingServer (multi-process).
- Caveat: PHP server performance is generally behind other languages; often it’s best as a web-facing client calling higher-performance Thrift backends over sockets or HTTP.
How do I install Thrift support for Perl and generate client stubs?- Install cpanm (App::cpanminus), then install the Thrift Perl lib from a GitHub mirror (e.g., sudo cpanm git://github.com/RandyAbernethy/ThriftPerl.git@0.10.0).
- Generate stubs: thrift --gen perl hello.thrift (creates gen-perl/).
- Generated files: helloSvc.pm (service), Constants.pm (constants), Types.pm (IDL types).
- Client pattern: use Thrift::Socket + Thrift::BufferedTransport + Thrift::BinaryProtocol; create helloSvcClient; open, call, close.
How do I write a simple Perl Thrift server and which pragmas are recommended?- Pragmas: use utf8 (for UTF-8 in source and protocol compatibility), use strict, and use warnings (safer, more robust server code).
- Implement a handler package (methods for your service, e.g., getMessage).
- Build server: Thrift::ServerSocket on a port (e.g., 9095), helloSvcProcessor with your handler, and a server such as Thrift::ForkingServer (or SimpleServer).
- Call $server->serve to run. Perl defaults to buffering + Binary protocol if you don’t pass factories.
What transports, protocols, and servers are available for Perl?- Transports: Thrift::Socket, Thrift::UnixSocket, Thrift::HttpClient, Thrift::MemoryBuffer; buffering/framing layers and SSL are supported.
- Protocols: Thrift::BinaryProtocol (native). For faster or alternative protocols, consider Thrift::XS (CPAN), which adds a faster Binary and a Compact protocol implementation.
- Servers: Thrift::SimpleServer (single-threaded) and Thrift::ForkingServer (one process per client). Multiplexing is supported.
How do I use Thrift with Python 2 and 3?- Install libraries with pip (Python 2) or pip3 (Python 3): pip install thrift or pip3 install thrift. The six library is used internally for cross-version compatibility.
- Generate stubs: thrift --gen py hello.thrift.
- Run examples under Python 2 (python ...) and, after pip3 install thrift, run clients under Python 3 (python3 ...). In many cases, the same code runs unmodified on both versions.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Programmer’s Guide to Apache Thrift ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Programmer’s Guide to Apache Thrift ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Programmer’s Guide to Apache Thrift ebook for free