Skip to main content

raceway

Minimal dependency injection, DI, system ideal for smaller systems that still need DI.

Overview

  • Defining: Define protocols and implementations.

  • Loading: Load registrations extracted from implementations into planner.

  • Starting: Run startup to create container from planner.

  • Wrapping: Use injector to wrap task handlers to prepare for injection.

  • Running: Create task for each “runner” and use it with the container to execute each injection enabled task handler.

Example

This library is meant to be used with an external framework but this example uses python’s stdlib threading to demonstrate how the pieces fit together.

import threading
import time
from dataclasses import dataclass
from typing import Protocol
from random import random

from raceway.extractor import configure_extractor
from raceway.registration import Registration
from raceway.planner import configure_planner
from raceway.starter import startup
from raceway.injector import configure_injector
from raceway.protocols import IContainer, ITask, IInjector, IExtractor

class ICrawlJob(ITask, Protocol):
    def get_link(self) -> str: ...

@dataclass
class CrawlJob(ICrawlJob):
    def __hash__(self):
        return id(self)
    def __eq__(self, other: object):
        return self is other
    id: int
    link: str
    def get_link(self) -> str:
        return self.link

class ICrawler(Protocol):
    def crawl(self, delay: float) -> str: ...

@dataclass
class CrawlerService(ICrawler):
    job: ICrawlJob
    def crawl(self, delay: float) -> str:
        return f"Crawled {self.job.get_link()} in {delay}s."

def run_job(crawler_api: ICrawler) -> None:
    delay = 3*random()
    time.sleep(delay)  # Blocking I/O (simulating a network request)
    print (crawler_api.crawl(delay))


def main():
    #
    # Setup
    #
    extractor_api =  configure_extractor()
    injector_api = configure_injector(extractor=extractor_api, task_proto=ICrawlJob)
    planner = configure_planner(task_proto=ICrawlJob)
    planner.queue_registration(
        ICrawler,
        Registration(
            CrawlerService, extractor_api.extract(CrawlerService), scope="task"
        ),
    )
    container_api = startup(planner=planner)

    # Prepare job runner for injection.
    wrapped_with_inject = injector_api.wrap_in_inject(run_job)

    #
    # Payloads
    #
    links = [
        "https://python.org",
        "https://docs.python.org",
        "https://peps.python.org",
        *[f"https://example.com/{v}" for v in range(20)],
    ]


    #
    # Start threads for each link
    #
    threads = []
    for link_id, link in enumerate(links):
        # Using `args` to pass positional arguments and `kwargs` for keyword arguments
        # ... in our case we are passing the args the inject function will need.
        job = CrawlJob(link_id, link)
        t = threading.Thread(
            target=wrapped_with_inject,
            args=(container_api, job),
        )
        threads.append(t)

    # Start each thread
    for t in threads:
        t.start()

    # Wait for all threads to finish
    for t in threads:
        t.join()


if __name__ == '__main__':
    main()

Metadata

Release files for raceway 0.2.10

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

Source distribution (sdist)

Source distribution for raceway 0.2.10
File Size Uploaded
raceway-0.2.10.tar.gz 16.6 kB Details

Built distribution (wheel)

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

Total release size: 31.4 kB

Release files / raceway-0.2.10.tar.gz

Download URL raceway-0.2.10.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a3b6887a2c5f4bbe1755a5f40ddf876ddf9f6b9e1160802d64cf42f4d1b989e3
BLAKE2b-256 checksum
How to use checksums
d0c53cbb9f48bf083c08989d4311eabce9151d25c9b71db500291324b137ce6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / raceway-0.2.10-py3-none-any.whl

Download URL raceway-0.2.10-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
661e2a01c0444e639ff3a048ff0e57df8416863f6c2dbbfe433fcb6fbb3bc38f
BLAKE2b-256 checksum
How to use checksums
a0184072cbc683ec0223687c5ddb3195458a633cbcfe5cd7252c588a8f172d5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

0.2.10 This release

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

1 release file

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2

2 release files

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