寻求可将Python函数提交至REST端点的框架及替代方案推荐
Hey there! I get what you're looking for—sending a Python function to a REST endpoint, having it execute, and getting a response back. Let's go through your options:
1. Ready-Made Frameworks (Close Fits)
There aren't many frameworks built specifically for this exact workflow, but a couple can be adapted with minimal work:
- FastAPI + HTTPX: If you're building the REST endpoint yourself, FastAPI makes it trivial to spin up a service that can handle function execution, and HTTPX is a modern client for sending the serialized function over HTTP. You'll just need to handle function serialization yourself (more on that below).
- Pyro5: While it's primarily an RPC framework, you can use its HTTP adapter to expose RPC endpoints as REST-like endpoints. It handles serialization out of the box, but it's a bit heavier if you only need basic REST functionality.
2. Lightweight Toolkit Combination (Flexible & Customizable)
If you want full control over the workflow, this stack of tools works perfectly together:
a. Serialize Python Functions: cloudpickle
Python's built-in pickle is limited and unsafe for untrusted data. cloudpickle extends it to serialize almost any Python object—including custom functions, closures, and even classes. It's ideal for converting your function into a sendable format:
import cloudpickle def add_numbers(a, b): return a + b # Turn the function into a byte stream serialized_func = cloudpickle.dumps(add_numbers)
b. Send to REST Endpoint: httpx
httpx is a modern, user-friendly HTTP client (think an upgraded requests). It supports both sync and async calls, making it easy to send your serialized function to the endpoint:
import httpx # Send the serialized function to your endpoint response = httpx.post( "http://your-server.com/run-task", content=serialized_func, headers={"Content-Type": "application/octet-stream"} ) # Deserialize and use the result result = cloudpickle.loads(response.content) print(result) # Outputs 3 if we ran add_numbers(1,2)
c. Build the REST Execution Endpoint: FastAPI
FastAPI is blazingly fast and intuitive for building REST services. Here's how to set up an endpoint that receives the serialized function, runs it, and sends back the result:
from fastapi import FastAPI, Request import cloudpickle app = FastAPI() @app.post("/run-task") async def run_task(request: Request): # Receive the serialized function serialized_data = await request.body() func = cloudpickle.loads(serialized_data) # Execute the function (adjust args/kwargs as needed) # Tip: You can also send args/kwargs alongside the function in a serialized dict execution_result = func(1, 2) # Serialize and return the result return cloudpickle.dumps(execution_result)
Critical Notes
- Security First:
cloudpickledeserialization is extremely risky with untrusted data! Always add authentication (API keys, OAuth2) to your endpoint, and only accept requests from trusted sources. - Parameter Passing: To send function arguments, serialize a dictionary like
{"func": my_func, "args": (1, 2), "kwargs": {"debug": True}}instead of just the function. - Async Support: Both FastAPI and HTTPX handle async functions seamlessly—just adjust your code with
async/awaitwhere needed.
内容的提问来源于stack exchange,提问作者JabberJabber

