golemfactory / pfaas

Python's very own gfaas implementation - an experiment

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pfaas

Function-as-a-Service on top of Golem Network in Python

This module allows you to distribute heavy-workload functions to the Golem Network. This is a sister project to gfaas.

⚠️ Disclaimer: Use with extra care as this package is highly experimental. ⚠️

Quick start

Currently, the only supported version of Python is version 3.8. Additionally, since this package is not yet released on pypi, you will need to manually install its dependencies

pip3 install yapapi

The usage is pretty straightforward. In your main Python module, e.g. main.py, import pfaas and annotate some heavy-workload function to be distributed on the Golem Network like so

from pfaas import remote_fn
import asyncio

@remote_fn()
def hello(msg: str) -> str:
    return msg.upper()

async def main(msg: str):
    resp = await hello(msg)
    print(f"in={msg}, out={resp}")

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    task = loop.create_task(main("hey there, yagna!"))

    try:
        asyncio.get_event_loop().run_until_complete(task)
    except (Exception, KeyboardInterrupt) as e:
        print(e)
        task.cancel()
        asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.3))

Now simply run as usual

python3 main.py

Notes about pfaas.remote_fn

When you annotate a function with pfaas.remote_fn attribute, it gets expanded into a full-fledged async function. So for instance, the following function

from pfaas import remote_fn

@remote_fn()
def hello(input: str) -> str

expands into

async def hello(input: str) -> str

Therefore, it is important to remember that you need to run the function in an async block.

Specifying Golem's configuration parameters

You can currently set the following configuration parameters directly via pfaas.remote_fn decorator:

  • (maximum) budget in NGNT (defaults to 100):
@remote_fn(budget=100.0)
def hello(input: str) -> str
  • timeout as timedelta (defaults to 10 minutes):
@remote_fn(timeout=timedelta(minutes=10))
def hello(input: str) -> str
  • subnet tag (defaults to "devnet-alpha.2"):
@remote_fn(subnet="devnet-alpha.2")
def hello(input: str) -> str

Of course, nobody stops you from setting any number of parameters at once

@remote_fn(budget=10.0, subnet="my_subnet")
def hello(input: str) -> str

Notes on running your app locally (for testing)

It is well known that prior to launching our app on some distributed network of nodes, it is convenient to first test the app locally in search of bugs and errors. This is also possible with pfaas. In order to force your app to run locally, simply pass run_local = True as argument to pfaas.remote_fn decorator

from pfaas import remote_fn

@remote_fn(run_local=True)
def hello(msg: str) -> str

Examples

A couple illustrative examples of how to use this module can be found in the examples/ directory.

About

Python's very own gfaas implementation - an experiment

License:GNU Lesser General Public License v3.0


Languages

Language:Python 97.0%Language:Dockerfile 3.0%