Skip to main content

A library that encapsulates callback functions within a continuation monad, utilizing a trampoline scheduler to enable stack-safe computations.

Project description

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)
    

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

continuationmonad-0.0.3.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

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

continuationmonad-0.0.3-py3-none-any.whl (40.4 kB view details)

Uploaded Python 3

File details

Details for the file continuationmonad-0.0.3.tar.gz.

File metadata

  • Download URL: continuationmonad-0.0.3.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for continuationmonad-0.0.3.tar.gz
Algorithm Hash digest
SHA256 50d4462c4221adbd4b46cbffd8a386870ded4f71e5aa0df256a7a22f8cd4e3db
MD5 fa7fdf6afda9ede2020ba112c120a383
BLAKE2b-256 ed2e41658defefa8565a2db6874e854f584b1773ff0b12dd8c91e8daad09f446

See more details on using hashes here.

File details

Details for the file continuationmonad-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for continuationmonad-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d7f44382122468bb82c88e5543ab4554dda516800a39ceefa2103adbcc4e2d5b
MD5 4fa00ce7e0902ae92ef8f9f9d1341ee3
BLAKE2b-256 8df72f08577ba79b08c5b23c30988f7448135f067998d3a467080c8894941a45

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