Skip to main content

Serve Python functions as web APIs

Project description

https://travis-ci.org/acroz/funcportal.svg?branch=master https://badge.fury.io/py/funcportal.svg

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.

Source Distribution

funcportal-0.2.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

funcportal-0.2.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file funcportal-0.2.0.tar.gz.

File metadata

  • Download URL: funcportal-0.2.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for funcportal-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a56f5df9e60188af7edc274befa65c58a6f01ba70ebb507dbc4181370bfeda5d
MD5 a7289a734a33028cfd0793a0cf1aa45f
BLAKE2b-256 2d2d12d6e29ebfcf3898c116e4f0892bff99cbf49553f7f0dcc5015e7fdacf29

See more details on using hashes here.

File details

Details for the file funcportal-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for funcportal-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5e475a8436a01565178707753690704a450b4f9ade68b6e39e7dd90b69c502d
MD5 8a100b3016c615541a58d41c56db51e7
BLAKE2b-256 75b02e6160197585a7aa18ac52d842be526437141d1b165f8d25776e09daec7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page