Skip to main content

A Queued State Machine Implementation for Python

Project description

qsm

A queued state machine implementation for Python.

A queued state machine is a small workflow runner where each state can schedule the next states to run. Instead of returning a single transition, states add state names to a queue. The machine dequeues one state at a time, executes it, and keeps going until the queue is empty.

Use append() for normal follow-up work. Use prepend() when a state needs to run before the work that is already waiting.

Installation

You can install this package with pip install qstate

Quickstart

from qsm import QSM, State, StateContext


class Start(State):
    def execute(self, ctx: StateContext) -> None:
        ctx.context["name"] = "World"
        ctx.queue.append("hello")
        ctx.queue.append("goodbye")


class Hello(State):
    def execute(self, ctx: StateContext) -> None:
        print(f"Hello {ctx.context['name']}!")


class Goodbye(State):
    def execute(self, ctx: StateContext) -> None:
        print(f"Goodbye {ctx.context['name']}!")


machine = QSM(initial_context={})
machine.state_map["initial_state"] = Start()
machine.state_map["hello"] = Hello()
machine.state_map["goodbye"] = Goodbye()
machine.loop()

Output:

Hello World!
Goodbye World!

The execution order is:

  1. start
  2. hello
  3. goodbye

Prepending Work

prepend() places a state at the front of the queue. This is useful for urgent or corrective work that should run before older queued states.

from qsm import State, StateContext


class Check(State):
    def execute(self, ctx: StateContext) -> None:
        if ctx.context["needs_login"]:
            ctx.queue.prepend("login")
        ctx.queue.append("continue")

If the queue already contains ["later"], this state can schedule login to run before later.

API

QSM

QSM(initial_context=None, initial_state="initial_state", max_queue_size=None)

QSM owns the state queue, the registered states, and shared workflow context.

  • machine.state_map maps state names to State instances.
  • machine.queue.append("state") schedules normal work.
  • machine.queue.prepend("state") schedules work at the front of the queue.
  • machine.get_next_state() dequeues the next state name and updates machine.current_state.
  • machine.execute_state("state") executes a registered state by name.
  • machine.execute_current_state() executes machine.current_state.
  • machine.loop() executes queued states until the queue is empty. It starts from a good state, it flushes the queue and enqueues the machine's initial state unless flush is False. Use flush=False to allow yourself to prime the queue prior to calling.

State

Create states by subclassing State and implementing execute().

from qsm import State, StateContext


class Example(State):
    def execute(self, ctx: StateContext):
        ...

execute() receives a StateContext with:

  • ctx.queue: the queue used to schedule more states.
  • ctx.context: the shared context object passed to QSM.

Notes

The queue stores state names as strings and is based on Python's queue.Queue locking model. It supports max-size blocking behavior, append(), and prepend().

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

qstate-1.0.1.tar.gz (138.0 kB view details)

Uploaded Source

Built Distribution

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

qstate-1.0.1-py3-none-any.whl (30.2 kB view details)

Uploaded Python 3

File details

Details for the file qstate-1.0.1.tar.gz.

File metadata

  • Download URL: qstate-1.0.1.tar.gz
  • Upload date:
  • Size: 138.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for qstate-1.0.1.tar.gz
Algorithm Hash digest
SHA256 76fe6b79b3adf18fec0d33f04775c199342367dac16e0974017f7719b300a982
MD5 782ddf06fe8992f69b909ca5ab7933e6
BLAKE2b-256 16de0dc68e8b072a8a023450811c5c681c75af93033162477e58b00f2a0d0f46

See more details on using hashes here.

File details

Details for the file qstate-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: qstate-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 30.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for qstate-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8cdae97e2d089b53d736f33b4aba392301f1d8c04d7974c37dccf4bb7d5d4def
MD5 f986a87a1382ba70fec5f6b5df1c963b
BLAKE2b-256 79c3bbfb15b46b6d7d2125da60a0ba4bcf961b7f85bcf2266873562bce813c4c

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