Skip to main content

tketool.core

tketool.core contains the shared configuration, file, cache, command, in-memory async messaging, and utility APIs used by the other tketool distributions.

pip install tketool.core
from tketool.core import ConfigManager, read_file, write_file
from tketool.core.cache.sqlite import init_db

Optional dependencies are grouped by feature:

pip install "tketool.core[storage]"
pip install "tketool.core[documents]"
pip install "tketool.core[console]"

In-memory async messaging

tketool.core.messaging provides a small replaceable publish/subscribe contract and a dependency-free in-memory adapter:

import asyncio

from tketool.core.messaging import create_message_bus


async def main() -> None:
    bus = create_message_bus()

    async def handle_order(message) -> None:
        print(message.message_id, message.payload)

    subscription = await bus.subscribe(
        "orders.created",
        handle_order,
        max_queue_size=100,
        handler_timeout=5.0,
    )

    receipt = await bus.publish("orders.created", {"order_id": "o-1"})
    assert receipt.subscriber_count == 1
    await bus.wait_idle()

    await subscription.close()
    await bus.close()


asyncio.run(main())

One subscription processes messages serially in FIFO order; different subscriptions run concurrently. A successful publish only confirms that the message was accepted by the subscriber queues. Handler failures are available through Subscription.last_error and an optional on_error callback.

This adapter is process-local and bound to one asyncio event loop. It does not persist messages, share work across processes, retry failures, or provide exactly-once delivery. Mutable payloads are passed by reference and should be treated as read-only. Use a durable broker adapter for work that must survive a restart.

Lifecycle and error handling

Use the bus as an async context manager when its lifetime matches one application scope:

import asyncio

from tketool.core.messaging import HandlerError, create_message_bus


async def report_error(error: HandlerError) -> None:
    print(error.message.message_id, error.exception)


async def process_document(message) -> None:
    print(message.payload["document_id"])


async def main() -> None:
    async with create_message_bus() as bus:
        await bus.subscribe(
            "documents.ready",
            process_document,
            handler_timeout=10.0,
            on_error=report_error,
        )
        await bus.publish("documents.ready", {"document_id": "d-1"})
        await bus.wait_idle()


asyncio.run(main())

publish() waits only for subscriber queues to accept the message. It returns a PublishReceipt; callback completion is observed with wait_idle(). Exceptions and async callback timeouts increment Subscription.failed_count, set Subscription.last_error, and invoke on_error. They are not sent back to the publisher and are not retried automatically.

close(drain=True) stops accepting new work and finishes accepted messages. close(drain=False) drops queued messages and cancels subscription workers. Synchronous callbacks are supported, but run on the event-loop thread and must not block; explicitly use asyncio.to_thread for blocking functions.

Delivery model

Property In-memory adapter behavior
Routing Exact topic match
Multiple subscribers Fan-out: every active subscriber receives the message
Ordering FIFO within one subscription
Concurrency Serial within one subscription; concurrent across subscriptions
Backpressure Bounded per-subscriber queues; publish() waits when full
Handler failure Recorded and isolated; no automatic retry
Persistence/replay None
Thread/process support One asyncio event loop in one process

If several workers should compete for one message instead of all receiving it, that is a work-queue/competing-consumer contract and is intentionally separate from this publish/subscribe API.

Verification and design documents

bash scripts/test-package.sh core

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tketool_core-2.0.1.tar.gz (109.4 kB view details)

Uploaded Source

Built Distribution

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

tketool_core-2.0.1-py3-none-any.whl (122.7 kB view details)

Uploaded Python 3

File details

Details for the file tketool_core-2.0.1.tar.gz.

File metadata

  • Download URL: tketool_core-2.0.1.tar.gz
  • Upload date:
  • Size: 109.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for tketool_core-2.0.1.tar.gz
Algorithm Hash digest
SHA256 bbbfd1e8a46b9880abbf09ec6d70b16fc1eb575ea07319fa07b3e9a99fdf403b
MD5 98f9ff41e7440ea613a443687e3e441e
BLAKE2b-256 ae4ccc3cf62212c9ceafe253a826145d02f94dadca833d561b9a28b9dbc04494

See more details on using hashes here.

File details

Details for the file tketool_core-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: tketool_core-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 122.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for tketool_core-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 61822b4300e64021d8f7580b03b5ce8a86f0e9e3bae70df6a2a3b6fc3bf147cd
MD5 66facbf5fa047f8931afb0012ecd7d18
BLAKE2b-256 6dcb0a8dd777c07257a4a982151d8817d270e7d9146156f5c014cb9d2c84eee6

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 files

1.4.3

2 files

1.4.0

2 files

1.3.5

2 files

1.3.4

2 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