Skip to main content

Python quickstart

  1. Setup your app folder
    mkdir reboot-hello && cd reboot-hello
  2. Install Reboot
    python3 -m venv ./.venv && source .venv/bin/activate && pip install reboot
  3. Create a .proto file
    mkdir -p ./api/hello/v1 && touch ./api/hello/v1/hello.proto
  4. Define your durable state data types and operations
    syntax = "proto3";

    package hello.v1;

    import "rbt/v1alpha1/options.proto";

    message Hello {
    option (rbt.v1alpha1.state) = {};
    repeated string messages = 1;
    }

    service HelloMethods {
    rpc Messages(MessagesRequest) returns (MessagesResponse) {
    option (rbt.v1alpha1.method).reader = {};
    }

    rpc Send(SendRequest) returns (SendResponse) {
    option (rbt.v1alpha1.method).writer = {};
    }
    }

    message MessagesRequest {}

    message MessagesResponse {
    repeated string messages = 1;
    }

    message SendRequest {
    string message = 1;
    }

    message SendResponse {}
  5. Create .py implementation file
    mkdir -p ./backend/src && touch ./backend/src/hello_servicer.py
  6. Implement HelloServicer
    from hello.v1.hello_rbt import (
    Hello,
    MessagesRequest,
    MessagesResponse,
    SendRequest,
    SendResponse,
    )
    from reboot.aio.contexts import ReaderContext, WriterContext

    class HelloServicer(Hello.Servicer):

    async def Messages(
    self,
    context: ReaderContext,
    state: Hello.State,
    request: MessagesRequest,
    ) -> MessagesResponse:
    return MessagesResponse(messages=state.messages)

    async def Send(
    self,
    context: WriterContext,
    state: Hello.State,
    request: SendRequest,
    ) -> SendResponse:
    state.messages.extend([request.message])
    return SendResponse()
  7. Create a main entrypoint
    touch ./backend/src/main.py
  8. Implement main.py
    import asyncio
    from hello.v1.hello_rbt import Hello
    from hello_servicer import HelloServicer
    from reboot.aio.applications import Application
    from reboot.aio.external import ExternalContext

    async def initialize(context: ExternalContext):
    hello = Hello.lookup('reboot-hello')

    await hello.idempotently().Send(
    context,
    message='Hello, World!',
    )


    async def main():
    application = Application(
    servicers=[HelloServicer],
    initialize=initialize,
    )

    await application.run()


    if __name__ == '__main__':
    asyncio.run(main())
  9. Create an .rbtrc file
    touch .rbtrc
  10. Add necessary flags to your .rbtrc file
    protoc api/
    protoc --python=backend/api/

    dev run --name=hello
    dev run --watch=backend/src/**/*.py
    dev run --python
    dev run --application=backend/src/main.py
  11. Run your app
    rbt dev run
  12. Query your app
    curl -XPOST http://localhost:9991/hello.v1.HelloMethods/Messages \
    -H "x-reboot-state-ref:hello.v1.Hello:reboot-hello"

Summary

In this example, you:

  1. Defined a Reboot durable state data type.
  2. Defined and implemented the operations on your data type.
  3. Built your Reboot app and ran it.

This example is somewhat contrived as we only created one instance of your Hello data type, but in practice you are free to make as many of these as you like!

So far, we've covered reader and writer methods but Reboot's real power is in transactions. Learn about transactions and all of the other available method kinds here.

Hopefully, your wheels are starting to spin thinking of all of the possibilities!

Next steps