Skip to content
Edit this page

Concurrency

Register ConcurrencyServiceProvider to run independent jobs with native asyncio.TaskGroup. Async callbacks run on the event loop. Synchronous callbacks run in native threads for blocking I/O. Results preserve sequence order or mapping keys.

from functools import partial

from titancore.concurrency import ConcurrencyServiceProvider
from titancore.facades import Concurrency

app.register(ConcurrencyServiceProvider())

# Inside the application's async lifecycle:
results = await Concurrency.run({"power": partial(pow, 2, 4)})

ConcurrencyManager is also usable directly. run accepts zero-argument callbacks, including callbacks that return awaitables. TaskGroup cancels sibling async jobs when one fails and reports an exception group. Cleanup waits for native thread callbacks to finish because running threads cannot be forcibly cancelled. Thread jobs copy the caller's context variables.

Use the parallel module for CPU work in separate processes and the pipeline module for dependent steps.