Skip to main content

remote-functions provides type enforced remotely run Python functions

Project description

remote-functions


Remote-functions provides type enforced remotely run Python functions. Using remote-functions, developers can run Python functions from any device with any programming language.

Key Features

  • Fast: Built on top of FastAPI.
  • Easy: Designed to be easy to use and learn.
  • Type Checking: Built-in type checking for function arguments.

Requirements

Python 3.8+

Installation

$ pip install remote-functions

Example

Server side

  • Create a file server.py with:
from remote_functions import remote, start


@remote(enforce_types=True)
def add(a: int, b: int):
    return a + b

if __name__ == '__main__':   
    start()

Client side

  • Create a file client.py with:
from remote_functions import Executor

api_url = "http://127.0.0.1:8000"
ex = Executor(api_url)

resp = ex.execute("add", a=2, b=3)
if resp.exit_code == 0:
    # function executed successfully
    print(resp.response)  # 5
elif resp.exit_code == 1:
    # function arguments were malformed
    print(resp.response)
elif resp.exit_code == 2:
    # function had an exception during execution
    print(resp.response)  # gives us the full traceback for easy debugging
Add authentication

If you want to protect your application from unauthorized access, you can enable key based authentication.

To enable authentication change your server.py file to:

from remote_functions import remote, start, Settings

settings = Settings()
settings.authorization = "super_secret_key"


@remote(enforce_types=True, settings=settings)
def add(a: int, b: int):
    return a + b


if __name__ == '__main__':
    start()

Then in client.py add the authorization argument

from remote_functions import Executor

api_url = "http://127.0.0.1:8000"
ex = Executor(api_url, authorization="super_secret_key")

resp = ex.execute("add", a=2, b=3)
if resp.exit_code == 0:
    # function executed successfully
    print(resp.response)  # 5
elif resp.exit_code == 1:
    # function arguments were malformed
    print(resp.response)
elif resp.exit_code == 2:
    # function had an exception during execution
    print(resp.response)  # gives us the full traceback for easy debugging

Run it

First start the server with:

$ python server.py

Then run client.py to test your remote function

$ python client.py
Deploy in production

To deploy your application for production you just have to slightly modify your server.py file by changing the host and port

from remote_functions import remote, start


@remote(enforce_types=True)
def add(a: int, b: int):
    return a + b


if __name__ == '__main__':
    start(host="0.0.0.0", port=80)

License

This project is licensed under the terms of the MIT license.

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

remote-functions-1.0.1.tar.gz (7.8 kB view hashes)

Uploaded Source

Built Distribution

remote_functions-1.0.1-py3-none-any.whl (7.6 kB view hashes)

Uploaded Python 3

Supported by

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