Skip to main content

Delta Cycle

Project description

Delta Cycle

DeltaCycle is a Python library for discrete event simulation (DES).

A simulation has two components: a collection of variables, and a collection of tasks. Variables represent the instantaneous state of the simulation. They may be organized into arbitrary data structures. Tasks define how that state evolves. They may appear concurrent, but are scheduled sequentially.

Task execution is subdivided into a sequence of slots. Slots are assigned a monotonically increasing integer value, called time. Multiple tasks may execute in the same slot, and therefore at the same time. The term "delta cycle" refers to a zero-delay subdivision of a time slot. It is the clockwork mechanism behind the illusion of concurrency.

Read the docs! (WIP)

Documentation Status

Features

  • Kernel: task scheduler
  • Task: coroutine wrapper
  • Synchronization Primitives:
    • Events
    • Semaphores
    • Credit Pools
    • Queues
    • Containers
  • Structured Concurrency:
    • Task Groups (parent/child hierarchy)
    • Interrupts
    • Exceptions
  • Model Variables:
    • Singular
    • Aggregate

Example

The following code simulates singing in the round. Four singers perform "Row, Row, Row Your Boat" in staggered fashion. Each singer is represented by a coroutine. A separator coroutine draws lines for readability. The coroutines are wrapped by tasks, and tasks are assigned a priority for precise ordering of concurrent events.

>>> from deltacycle import *

>>> song = [
...     "Row, row, row your boat",
...     "Gently down the stream",
...     "Merrily, merrily, merrily, merrily",
...     "Life is but a dream",
... ]

>>> async def singer(name: str, delay: int = 0):
...     await sleep(delay)
...     for line in song:
...         print(f"{now()}{name}: {line:34} ♫")
...         await sleep(1)

>>> async def separator():
...     while True:
...         print(f"{now()}  ♫---------------------------------------♫")
...         await sleep(1)

>>> async def main():
...     t = create_task(separator(), priority=-1)
...
...     async with TaskGroup() as tg:
...         tg.create_task(singer("A", delay=0), priority=1)
...         tg.create_task(singer("B", delay=1), priority=2)
...         tg.create_task(singer("C", delay=2), priority=3)
...         tg.create_task(singer("D", delay=3), priority=4)
...
...     t.interrupt()  # Interrupt separator task
...
...     return "Bow / Curtsy"

>>> r = run(main())
0  ♫---------------------------------------♫
0  ♫ A: Row, row, row your boat            ♫
1  ♫---------------------------------------♫
1  ♫ A: Gently down the stream             ♫
1  ♫ B: Row, row, row your boat            ♫
2  ♫---------------------------------------♫
2  ♫ A: Merrily, merrily, merrily, merrily ♫
2  ♫ B: Gently down the stream             ♫
2  ♫ C: Row, row, row your boat            ♫
3  ♫---------------------------------------♫
3  ♫ A: Life is but a dream                ♫
3  ♫ B: Merrily, merrily, merrily, merrily ♫
3  ♫ C: Gently down the stream             ♫
3  ♫ D: Row, row, row your boat            ♫
4  ♫---------------------------------------♫
4  ♫ B: Life is but a dream                ♫
4  ♫ C: Merrily, merrily, merrily, merrily ♫
4  ♫ D: Gently down the stream             ♫
5  ♫---------------------------------------♫
5  ♫ C: Life is but a dream                ♫
5  ♫ D: Merrily, merrily, merrily, merrily ♫
6  ♫---------------------------------------♫
6  ♫ D: Life is but a dream                ♫
7  ♫---------------------------------------♫

>>> print(r)
Bow / Curtsy

Installing

DeltaCycle is available on PyPI:

$ pip install deltacycle

It requires Python 3.12+

Developing

DeltaCycle's repository is on GitHub:

$ git clone https://github.com/cjdrake/deltacycle.git

It is 100% Python, and has no runtime dependencies. Development dependencies are listed in requirements-dev.txt.

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

deltacycle-0.31.0.tar.gz (30.4 kB view details)

Uploaded Source

Built Distribution

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

deltacycle-0.31.0-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file deltacycle-0.31.0.tar.gz.

File metadata

  • Download URL: deltacycle-0.31.0.tar.gz
  • Upload date:
  • Size: 30.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for deltacycle-0.31.0.tar.gz
Algorithm Hash digest
SHA256 6988f25cecec26a9ce713b2ef1f1c5e5d27f43a6e717c172e3ccdb6012fbbbd5
MD5 e20a4b572f97eb87acb5fa2a55bd0653
BLAKE2b-256 9e7fbce385b3a3231f5ac51d60b1f3c1e80b18c2292a95d2f8b1037631ac8beb

See more details on using hashes here.

File details

Details for the file deltacycle-0.31.0-py3-none-any.whl.

File metadata

  • Download URL: deltacycle-0.31.0-py3-none-any.whl
  • Upload date:
  • Size: 21.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for deltacycle-0.31.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4ab6b8fde1c5de69fa7e2d8ef2baf5343506a5a5b29b5f1d6fecb8fa1c947f3
MD5 dba1e3f4004f66439415a4e68f860cf9
BLAKE2b-256 f032308a4074c59b4282942a532e24d6e929231c2ecedf685bbe4376867dd994

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