Skip to main content

A library for building runnable asynchronous trees

Project description

A simple library for building runnable async trees. Trees are a web of interconnected Nodes, which contain code to be run. A node can only start executing once all it's parents have finished running.

Features

  • The number of workers is automatically managed - although you can parametrize this
  • Trees can have any shape - including multiple roots - and can be dynamically altered during their runtime
  • State can be passed between nodes manually (as lambda functions) or via forwarding
  • Yielding coroutines produce Chunk objects that wrap intermediate results with the node's UUID

Installation

  • pip install grafo to install on your environment
  • pytest to run tests, add -s flag for tests to run print statements

Use

Basic tree execution

async def my_coroutine():
    return "result"

root_node = Node(coroutine=my_coroutine, uuid="root")
child_node = Node(coroutine=my_coroutine, uuid="child")

await root_node.connect(child_node)

executor = TreeExecutor(uuid="My Tree", roots=[root_node])
result = await executor.run()

Yielding intermediate results

async def yielding_coroutine():
    for i in range(3):
        yield f"progress {i}"
    yield "completed"

node = Node(coroutine=yielding_coroutine)
executor = TreeExecutor(roots=[node])

async for item in executor.yielding():
    if isinstance(item, Node):
        print(f"Node {item.uuid} completed")
    else:  # Chunk
        print(f"Intermediate: {item.output}")

Evaluating coroutine kwargs during runtime (manual forwarding)

node = Node(
    coroutine=my_coroutine,
    kwargs=dict(
        my_arg=lambda: get_dynamic_value()
    )
)

Forwarding output between nodes (automatic forwarding)

async def producer():
    return "data"

async def consumer(data: str):
    return f"processed_{data}"

node_a = Node(coroutine=producer, uuid="producer")
node_b = Node(coroutine=consumer, uuid="consumer")

await node_a.connect(node_b, forward_as="data")
# node_b will receive node_a's output as the 'data' argument

Type validation with generics

node = Node[str](coroutine=my_string_coroutine)
# The node will validate that the coroutine returns a string

Developer's Zen

  1. Follow established nomenclature: a Node is a Node.
  2. Syntax sugar is sweet in moderation.
  3. Give the programmer granular control.

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

grafo-0.3.1.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

grafo-0.3.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file grafo-0.3.1.tar.gz.

File metadata

  • Download URL: grafo-0.3.1.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for grafo-0.3.1.tar.gz
Algorithm Hash digest
SHA256 69be950e2e4cc35b94c333c48de689c8e05d1d672e25f0688f6c2d848d3a9a47
MD5 d4d319242d02a8665977d7adfdc73184
BLAKE2b-256 8471325a1e5560a42aa6151df65bcb5e1e1b9171213f0ba82f7628b61c349faa

See more details on using hashes here.

File details

Details for the file grafo-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: grafo-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for grafo-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d1da37c9e8a484c28c02747545fbbeac72fe3e67b64cd9a4d23171c99b35dc89
MD5 d184059c74f0daa3661e1b9732cd321e
BLAKE2b-256 7be20c4d6c24416d0b1225ca3c929a8666baa29fbad3c61c1b926b99076793b2

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