Serve Python functions as web APIs
Project description
funcportal runs your Python functions as a web API, with no code changes required.
Usage
Given a Python module like code.py below:
# code.py def hello(name): return f'Hello, {name}!'
You can serve the function hello() as a web API with funcportal on the command line:
$ funcportal server code:hello
and you can then make HTTP POST requests to it, for example with the Python requests library:
>>> import requests >>> response = requests.post( >>> 'http://localhost:5000/hello', >>> json={'name': 'Jane'} >>> ) >>> print(response.status_code) 200 >>> print(response.json()) {'result': 'Hello, Jane!'}
Alternatively, use a configuration file to specify the functions to serve and their endpoints. The configuration file is YAML formatted:
routes: - module: code function: hello endpoint: /hello - module: code function: other endpoint: /other
As with the command line interface, the module and function indicate the code to be run, and the endpoint is the address on the server that the API will be run.
Load endpoints from the configuration file with the -c/--config command line option:
$ funcportal server -c config.yaml
Asynchronous execution
When executing longer running function calls, you won’t want to hold open the HTTP connection for a long time, as it increases the risk of failure (as well as making management of server resources more difficult). To avoid this, use funcportal’s asynchronous execution feature. To enable asynchronous execution, set the async flag to true for a route in the configuration file:
routes: - module: code function: slow endpoint: /slow async: true
Important: To execute functions asynchonously, you need to have redis server installed and running, and then also run one or more funcportal worker processes separately from the server process(es):
$ funcportal worker
Then, when you call this endpoint, instead of waiting until the function has finished running and returning the result (if any), a response will be returned immediately with a token that can be redeemed later for the result:
>>> response = requests.post( >>> 'http://localhost:5000/slow', >>> json={'input': 4} >>> ) >>> print(response.status_code) 202 >>> print(response.json()) {'result_token': '3bf409d0-4b91-4e75-87e4-c377f2f9dbf6'}
You can then poll the original endpoint plus the result token with an HTTP GET to retrieve the result when ready. Before the result is ready, a 404 NOT FOUND status is returned:
>>> response = requests.get( >>> 'http://localhost:5000/slow/3bf409d0-4b91-4e75-87e4-c377f2f9dbf6' >>> ) >>> print(response.status_code) 404 >>> print(response.json()) {'error': 'Job result not available.'}
Once the job is finished, a 200 OK status is returned along with the result:
>>> response = requests.get( >>> 'http://localhost:5000/slow/3bf409d0-4b91-4e75-87e4-c377f2f9dbf6' >>> ) >>> print(response.status_code) 200 >>> print(response.json()) {'result': 79}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size funcportal-0.2.0-py3-none-any.whl (7.7 kB) | File type Wheel | Python version py3 | Upload date | Hashes View |
Filename, size funcportal-0.2.0.tar.gz (7.1 kB) | File type Source | Python version None | Upload date | Hashes View |
Hashes for funcportal-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5e475a8436a01565178707753690704a450b4f9ade68b6e39e7dd90b69c502d |
|
MD5 | 8a100b3016c615541a58d41c56db51e7 |
|
BLAKE2-256 | 75b02e6160197585a7aa18ac52d842be526437141d1b165f8d25776e09daec7f |