Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Quickstart — Resonate example

examples-ci status

Quickstart | Resonate Python SDK

The function you are about to activate

A countdown as a loop. Simple, but the function can run for minutes, hours, or days, despite restarts.

from __future__ import annotations

import asyncio
import os
from typing import TYPE_CHECKING

from resonate.resonate import Resonate

if TYPE_CHECKING:
    from resonate.context import Context


async def countdown(ctx: Context, count: int, delay: int) -> None:
    for i in range(count, 0, -1):
        # Run a function, persist its result
        await ctx.run(ntfy, i)
        # Sleep
        await ctx.sleep(delay)
    print("Done!")


async def ntfy(_: Context, i: int) -> None:
    print(f"Countdown: {i}")


async def main() -> None:
    r = Resonate(url=os.environ.get("RESONATE_URL", "http://localhost:8001"))
    r.register(countdown)
    r.register(ntfy)
    await asyncio.Event().wait()


if __name__ == "__main__":
    asyncio.run(main())

Steps to run

1. Install the Resonate Server & CLI

brew install resonatehq/tap/resonate

2. Install the Resonate SDK

pip install resonate-sdk

3. Start the server

resonate dev

4. Start the worker

python countdown.py

5. Activate the function

Activate the function with execution ID countdown.1:

resonate invoke countdown.1 --func countdown --arg 5 --arg 60

Result

You will see the countdown in the terminal

python countdown.py
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Done!

What to try

After starting the function, inspect the current state of the execution using the resonate tree command. The tree command visualizes the call graph of the function execution as a graph of durable promises.

resonate tree countdown.1

Now try killing the worker mid-countdown and restarting. The countdown picks up right where it left off without missing a beat.

Next steps

About

Get started with Resonate's Python SDK with this quickstart countdown example

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages