Skip to main content

Fast-forward asyncio event loop time (in tests)

CI codecov Coverage Status pre-commit

What is this?

Fake the flow of time in asyncio event loops. The effects of time removal can be seen from both sides:

  • From the event loop's (i.e. your tests') point of view, all external activities, such as synchronous executor calls (thread pools) and i/o with sockets, servers, files, happen in zero amount of the loop time — even if it takes some real time. This hides the code overhead and network latencies from the time measurements, making the loop time sharply and predictably advancing in configured steps.

  • From the observer's (i.e. your personal) point of view, all activities of the event loop, such as sleeps, events/conditions waits, timeouts, "later" callbacks, happen in near-zero amount of the real time (due to the usual code execution overhead). This speeds up the execution of tests without breaking the tests' time-based design, even if they are designed to run in seconds or minutes.

For the latter case, there are a few exceptions when the event loop's activities are synced with the true-time external activities, such as thread pools or i/o, so that they spend the real time above the usual code overhead (if configured).

The library was originally developed for Kopf, a framework for Kubernetes Operators in Python, which actively uses asyncio tests in pytest (≈7000 unit-tests in ≈2 minutes). You can see how this library changes and simplifies the tests in Kopf's PR #881.

What is it not?

It speeds up tests based on the flow of time, in particular various kinds of timers, timeouts, sleeps, delays, rate limiters — both in tests and in the system under test.

It does NOT speed up tests that are simply slow with no explicit delays, such as those involving the local/loopback network communication, heavy algorithmical compute, slow data moving or processing, etc. These activities take their fair time and cannot be time-compacted.

It does NOT speed up time-based tests using the synchronous primitives and the wall-clock time; looptime compacts only the asyncio time.

Examples

Here, we assume that the async tests are supported. For example, use pytest-asyncio:

pip install pytest-asyncio
pip install looptime

Nothing is needed to make async tests run with the fake time, it just works:

import asyncio
import pytest


@pytest.mark.asyncio
async def test_me():
    await asyncio.sleep(100)
    assert asyncio.get_running_loop().time() == 100
pytest --looptime

The test will be executed in approximately 0.01 seconds, while the event loop believes it is 100 seconds old.

If the command line or ini-file options for all tests is not desirable, individual tests can be marked for fast time forwarding explicitly:

import asyncio
import pytest


@pytest.mark.asyncio
@pytest.mark.looptime
async def test_me():
    await asyncio.sleep(100)
    assert asyncio.get_running_loop().time() == 100
pytest

Under the hood, the library solves some nuanced situations with time in tests. See "Nuances" below for more complicated (and nuanced) examples.

Documentation

For more tricks and options, see the full documentation.

Metadata

Release files for looptime 0.8

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

Source distribution (sdist)

Source distribution for looptime 0.8
File Size Uploaded
looptime-0.8.tar.gz 40.7 kB Details

Built distribution (wheel)

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

Total release size: 62.3 kB

Release files / looptime-0.8.tar.gz

Download URL looptime-0.8.tar.gz
Size 40.7 kB
Tags Source
SHA-256 checksum
How to use checksums
539578e61324fb2b6f11e427bdd348b356920bbf370c749e6c535fc058e8e7be
BLAKE2b-256 checksum
How to use checksums
d14624cfe8d29810eda4956f0bb48fe42c6e080597dc4e0b012df6255d7b4293
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release files / looptime-0.8-py3-none-any.whl

Download URL looptime-0.8-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3483b368962b0145f8f4be7383a595e7fa0f341b1f58bd8c897fa4dd06cb0370
BLAKE2b-256 checksum
How to use checksums
9a70fcbe4077e794925c90d3a55fcc9d85199300eaf1bc250290c388ef14259d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release history Release notifications | RSS feed

0.9

2 release files

This release

0.8 This release

2 release files

0.7

2 release files

0.6

2 release files

0.5

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

0.0

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