Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Serve Python functions as web APIs

Project description

https://travis-ci.org/acroz/funcportal.svg?branch=master

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


Release history Release notifications

This version
History Node

0.2.0

History Node

0.1.1

History Node

0.1.0

History Node

0.0.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
funcportal-0.2.0-py3-none-any.whl (7.7 kB) Copy SHA256 hash SHA256 Wheel py3 Apr 16, 2018
funcportal-0.2.0.tar.gz (7.1 kB) Copy SHA256 hash SHA256 Source None Apr 16, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page