ThriftPy2
ThriftPy2 is a pure Python implementation of the Apache Thrift protocol, version 0.6.0. It allows developers to parse Thrift IDL files and create RPC clients/servers dynamically without code generation or compilation. The library maintains an active development status with regular updates, including recent beta releases leading to stable versions.
Warnings
- deprecated Support for Tornado-based servers and clients has been deprecated in v0.6.0. Users relying on `thriftpy2.tornado` modules should migrate to `asyncio` or other HTTP transports.
- gotcha When migrating from the original `thriftpy` library, simply changing import statements from `import thriftpy` to `import thriftpy2` might cause issues if other parts of your code still expect the `thriftpy` namespace. While `thriftpy2` is designed for compatibility, direct renaming is safer.
- gotcha If you install `thriftpy2` in a PyPy virtual environment, `pip` might generate a universal wheel without Cython extensions. Using this cached wheel later in a CPython environment can lead to `ModuleNotFoundError: No module named 'thriftpy2.protocol.cybin'` because CPython expects the Cython-compiled modules.
- gotcha When dynamically loading Thrift IDL files using `thriftpy2.load()`, if you do not provide the `module_name` argument, the generated Thrift objects cannot be pickled. This can cause issues with serialization in distributed systems or caching.
Install
-
pip install thriftpy2 -
pip install cython thriftpy2
Imports
- thriftpy2
import thriftpy2
- make_server
from thriftpy2.rpc import make_server
- make_aio_client
from thriftpy2.rpc import make_aio_client
- thriftpy
import thriftpy2 as thriftpy
Quickstart
import asyncio
import thriftpy2
from thriftpy2.rpc import make_aio_client
import os
# Define a simple Thrift service IDL in a temporary file
THRIFT_FILE_PATH = "pingpong.thrift"
with open(THRIFT_FILE_PATH, "w") as f:
f.write("service PingPong {\n string ping(),\n}")
# Load the thrift file dynamically
pingpong_thrift = thriftpy2.load(THRIFT_FILE_PATH, module_name="pingpong_thrift")
async def main():
print("Attempting to create ThriftPy2 async client...")
client = None
try:
# For this quickstart, we'll demonstrate client instantiation and a call pattern.
# Note: This client will attempt to connect to '127.0.0.1:6000'.
# A running ThriftPy2 server on this address would be required for a successful RPC call.
# This example focuses on demonstrating the client API, not a full RPC pair.
client = await make_aio_client(
pingpong_thrift.PingPong,
'127.0.0.1',
6000,
timeout=1000 # Milliseconds for connection/read timeout
)
print("Client created. Attempting to call ping()... (This will likely fail without a running server)")
# Example of calling a service method
# result = await client.ping()
# print(f"Ping result: {result}")
except Exception as e:
print(f"Error setting up client (expected if no server is running at 127.0.0.1:6000): {e}")
finally:
if client:
client.close()
# Clean up the dummy thrift file
if os.path.exists(THRIFT_FILE_PATH):
os.remove(THRIFT_FILE_PATH)
if __name__ == '__main__':
asyncio.run(main())