Skip to main content

Continuation-Monad

A Python library implementing stack-safe continuations based on schedulers, ensuring deadlock-free asynchronous computations.

Features

  • Trampoline-based: Stack-safe execution of the continuation monad through trampolining
  • Continuation certificate: Ensures the execution of a Flowable completes, albeit with some computational overhead.
  • Scheduler-based: Explicit control over execution context through different scheduler implementations
  • Composable operations: Monadic operators for clean functional pipelines

Installation

You can install Continuation-Monad using pip:

pip install continuationmonad

Example

import continuationmonad


def count_down(count: int):
    print(f'{count=}')

    if count == 0:
        return continuationmonad.from_(count)
    
    else:
        # schedule recursive call on the trampoline
        return continuationmonad.tail_rec(lambda: count_down(count - 1))

# runs continuation and returns 0
result = continuationmonad.run(
    count_down(5)
)

Schedulers and Trampolines

The library provides the following scheduler implementations

  • init_main_scheduler: Initializes a scheduler that runs in the main thread. It starts execution via the run() method, which blocks until stop() is called to terminate the scheduler.
  • init_event_loop_scheduler: Creates a scheduler that operates on a dedicated background thread. This is useful for offloading tasks from the main thread or isolating execution contexts.
    scheduler = continuationmonad.init_event_loop_scheduler()
    
  • init_trampoline: Returns a lightweight scheduler that only implements the schedule() method. It does not support delayed execution methods like schedule_relative() or schedule_absolute(). This scheduler runs tasks immediately in a loop until its queue is exhausted.

Operations

Creating Continuation Monads

  • defer - creates a continuation monad that defers the subscription until a source is connected (see defer example)
  • from_: Create a continuation monad from a value:
    c = continuationmonad.from_(5)
    
  • get_trampoline - retrieve trampoline associated with the continuation monad
    c = continuationmonad.get_trampoline()
    
  • schedule_on - schedule elements emitted by the source on a dedicated scheduler
    c = continuationmonad.schedule_on(scheduler)
    
  • schedule_trampoline - schedule item on trampoline
    c = continuationmonad.schedule_trampoline()
    
  • tail_rec - recursively call a function on trampoline

Transforming operators

  • connect - connects the continuation monad to deferred one or multiple subscribers (see defer example)
  • flat_map - apply a function to the item emitted by the source and flattens the result
  • map - map the item emitted by the source by applying the given function

Combining operators

  • zip - create a new continuation monad from two (or more) continuation monads by combining their items in a tuple
    c = continuationmonad.zip((
        continuationmonad.from_(1),
        continuationmonad.from_(2),
    ))
    

Other operators

  • fork - runs the continuation monad on a specified trampoline (see defer example)
    c = continuationmonad.fork(
        source=source,
        scheduler=scheduler,
        weight=1,               # specify continuation weight
    )
    
  • run - runs the continuation monad on a new trampoline and returns its result
    # execute a continuation monad
    result = continuationmonad.run(source)
    

Metadata

Release files for continuationmonad 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for continuationmonad 0.0.3
File Size Uploaded Provenance
continuationmonad-0.0.3.tar.gz 19.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for continuationmonad 0.0.3
File Interpreter ABI Platform
continuationmonad-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 60.0 kB

Release files / continuationmonad-0.0.3.tar.gz

Download URL continuationmonad-0.0.3.tar.gz
Size 19.6 kB
Tags Source
SHA-256 checksum
How to use checksums
50d4462c4221adbd4b46cbffd8a386870ded4f71e5aa0df256a7a22f8cd4e3db
BLAKE2b-256 checksum
How to use checksums
ed2e41658defefa8565a2db6874e854f584b1773ff0b12dd8c91e8daad09f446
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / continuationmonad-0.0.3-py3-none-any.whl

Download URL continuationmonad-0.0.3-py3-none-any.whl
Size 40.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d7f44382122468bb82c88e5543ab4554dda516800a39ceefa2103adbcc4e2d5b
BLAKE2b-256 checksum
How to use checksums
8df72f08577ba79b08c5b23c30988f7448135f067998d3a467080c8894941a45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for continuationmonad 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page