later

26.1.1 · active · verified Thu Apr 16

Later is a Python library developed by Meta Platforms, Inc., offering a collection of "batteries included" for building `asyncio` services. It provides tools for unittesting asynchronous code, managing `asyncio` tasks, and handling asynchronous events. The current version is 26.1.1, and it maintains an active development status, with releases focusing on robust `asyncio` patterns.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates using `later.Watcher` to manage `asyncio` tasks and `later.cancel` for safe task cancellation. It spawns two worker coroutines, cancels one after a delay, and then waits for all managed tasks to finish.

import asyncio
from later import as_task, Watcher, cancel

async def worker(name, delay):
    print(f"{name}: Starting work...")
    try:
        await asyncio.sleep(delay)
        print(f"{name}: Work done.")
    except asyncio.CancelledError:
        print(f"{name}: Work cancelled.")

async def main():
    watcher = Watcher()
    task1 = await watcher.spawn(worker('Worker 1', 2))
    task2 = await watcher.spawn(worker('Worker 2', 5))

    await asyncio.sleep(1) # Let tasks start
    print("Main: Cancelling Worker 1")
    await cancel(task1)

    # Wait for all tasks in the watcher to complete or be cancelled
    await watcher.join()
    print("Main: All tasks managed by Watcher have completed.")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →