High-level Python API for the New Golem
Golem Python API
yapapi is available as a PyPI package.
You can install it through
pip install yapapi
Or if your project uses
poetry you can add it to your dependencies like this:
poetry add yapapi
Local development setup
poetry to manage its dependencies and provide a runner for common tasks.
If you don't have
poetry available on your system then follow its installation instructions before proceeding.
Verify your installation by running:
To install the project's dependencies run:
poetry looks for the required Python version on your
PATH and creates a virtual environment for the project if there's none active (or already configured by Poetry).
All of the project's dependencies will be installed to that virtual environment.
If you'd like to run the
yapapi integration test suite locally then you'll need to install an additional set of dependencies separately by running:
poetry install -E integration-tests
What's Golem, btw?
Golem is a global, open-source, decentralized supercomputer that anyone can access. It connects individual machines - be that laptops, home PCs or even data centers - to form a vast network, the purpose of which is to provide a way to distribute computations to its provider nodes and allow requestors to utilize its unique potential - which can lie in its combined computing power, the geographical distribution or its censorship resistance.
Golem's requestor setup
Golem's requestor-side configuration consists of two separate components:
yagnadaemon - your node in the new Golem network, responsible for communication with the other nodes, running the market and providing easy access to the payment mechanisms.
- the requestor agent - the part that the developer of the specific Golem application is responsible for.
The daemon and the requestor agent communicate using three REST APIs which
yapapi - Golem's Python high-level API - aims to abstract to large extent to make
application development on Golem as easy as possible.
How to use this API?
- prepare your payload - this needs to be a Docker image containing your application
that will be executed on the provider's end. This image needs to have its volumes
mapped in a way that will allow the supervisor module to exchange data (write and
read files) with it. This image needs to be packed and uploaded into Golem's image repository
using our dedicated tool -
- create your requestor agent - this is where
yapapicomes in. Utilizing our high-level API, the creation of a requestor agent should be straightforward and require minimal effort. You can use examples contained in this repository as references, the directory
examples/hello-world/contains minimal examples of fully functional requestor agents and is therefore the best place to start exploring.
There are a few components that are crucial for any requestor agent app:
The heart of the high-level API is the
Golem class (
yapapi.Golem), which serves as
the "engine" of a requestor agent.
Golem is responsible for finding providers interested in
the jobs you want to execute, negotiating agreements with them and processing payments. It also
implements core functionality required to execute commands on providers that have signed such agreements.
Golem provides two entry points for executing jobs on the Golem network, corresponding to the
two basic modes of operation of a requestor agent:
execute_tasksallows you to submit a task-based job for execution. Arguments to this method must include a sequence of independent tasks (units of work) to be distributed among providers, a payload (a VM image) required to compute them, and a worker function, which will be used to convert each task to a sequence of steps to be executed on a provider. You may also specify the timeout for the whole job, the maximum number of providers used at any given time, and the maximum amount that you want to spend.
run_serviceallows you, as you probably guessed, to run a service on Golem. Instead of a task-processing worker function, an argument to
run_serviceis a class (a subclass of
yapapi.Service) that implements the behaviour of your service in various stages of its lifecycle (when it's starting, running etc.). Additionally, you may specify the number of service instances you want to run and the service expiration datetime.
Prior to version
0.6.0, only task-based jobs could be executed. For more information on both types of jobs please refer to our handbook.
The worker will most likely be the very core of your task-based requestor app.
You need to define this function in your agent code and then you pass it (as the value of
worker parameter) to the
execute_tasks method of
The worker receives a work context (
yapapi.WorkContext) object that serves
as an interface between your script and the execution unit within the provider.
Using the work context, you define the steps that the provider needs to execute in order
to complete the job you're giving them - e.g. transferring files to and from the provider
or running commands within the execution unit on the provider's end.
Depending on the number of workers, and thus, the maximum number of providers that
execute_tasks utilizes in parallel, a single worker may tackle several tasks
and you can differentiate the steps that need to happen once
per worker run, which usually means once per provider node - but that depends on the
exact implementation of your worker function - from those that happen for each
task. An example of the former would be an upload of a source
file that's common to each task; and of the latter - a step that triggers the
processing of the file using a set of parameters specified for a particular task.
The task (
yapapi.Task) object describes a unit of work that your application needs
to carry out.
Golem will feed an instance of your worker - bound to a single provider node -
Task objects. The worker will be responsible for completing those tasks. Typically,
it will turn each task into a sequence of steps to be executed in a single run
of the execution script on a provider's machine, in order to compute the task's result.
An example task-based Golem application, using a minimal Docker image
(Python file with the example and the Dockerfile for the image reside in
import asyncio from typing import AsyncIterable from yapapi import Golem, Task, WorkContext from yapapi.log import enable_default_logger from yapapi.payload import vm async def worker(context: WorkContext, tasks: AsyncIterable[Task]): async for task in tasks: script = context.new_script() future_result = script.run("/bin/sh", "-c", "date") yield script task.accept_result(result=await future_result) async def main(): package = await vm.repo( image_hash="d646d7b93083d817846c2ae5c62c72ca0507782385a2e29291a3d376", ) tasks = [Task(data=None)] async with Golem(budget=1.0, subnet_tag="devnet-beta") as golem: async for completed in golem.execute_tasks(worker, tasks, payload=package): print(completed.result.stdout) if __name__ == "__main__": enable_default_logger(log_file="hello.log") loop = asyncio.get_event_loop() task = loop.create_task(main()) loop.run_until_complete(task)
It's possible to set various elements of
yagna configuration through environment variables.
yapapi currently supports the following environment variables:
YAGNA_ACTIVITY_URL, URL to
yagnaactivity API, e.g.
YAGNA_API_URL, base URL to
yagnaREST API, e.g.
yagnaapp key to be used, e.g.
YAGNA_MARKET_URL, URL to
yagnamarket API, e.g.
YAGNA_PAYMENT_NETWORK, Ethereum network name for
yagnato use, e.g.
YAGNA_PAYMENT_DRIVER, payment driver name for
yagnato use, e.g.
YAGNA_PAYMENT_URL, URL to
yagnapayment API, e.g.
YAGNA_SUBNET, name of the
yagnasub network to be used, e.g.
YAPAPI_USE_GFTP_CLOSE, if set to a truthy value (e.g. "1", "Y", "True", "on") then
gftpto close files when there's no need to publish them any longer. This may greatly reduce the number of files kept open while
yapapiis running but requires
yagna0.7.3 or newer, with older versions it will cause errors.
Release history Release notifications | RSS feed
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 yapapi-0.7.0-py3-none-any.whl (107.7 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size yapapi-0.7.0.tar.gz (95.1 kB)||File type Source||Python version None||Upload date||Hashes View|