Skip to main content

Asyncio Time Travel Loop

Intro

Asyncio Time Travel Loop allows you to test asyncio code that waits or sleeps, without actually waiting or sleeping.

At the same time, you don't have to bother thinking about how the time advances. Your code should work exactly the same with TimeTravelLoop as it works with a normal asyncio events loop.

Example: Assume that you have a code that waits 1000 seconds, and you want to tests that code. Instead of actually waiting 1000 seconds, you could use TimeTravelLoop:

import asyncio
from asyncio_time_travel import TimeTravelLoop

SLEEP_TIME = 1000

tloop = TimeTravelLoop()

async def inner_coro():
    # Sleep for a long time:
    await asyncio.sleep(SLEEP_TIME, loop=tloop)

tloop.run_until_complete(inner_coro())

This code completes immediately.

See some of the tests for more advanced examples of what TimeTravelLoop can do.

Installation

Run:

pip install asyncio-time-travel

You can also find the package at https://pypi.python.org/pypi/asyncio-time-travel .

Tests

Run (Inside asyncio_time_travel dir):

pytest

If you haven't yet heard of pytest, it's your lucky day :)

How does this work?

TimeTravelLoop source is based on the source code of asyncio.test_utils.TestLoop.

For each _run_once iteration of the loop, the following is done:

  • Loop events are executed.
  • Time is advanced to the closest scheduled event.

Using this method your code never waits, and at the same time the events execute in the correct order.

Limitations

TimeTravelLoop is meant to be used with your tests, not for production code. In particular, if your loop interacts with external events, bending time is not a good idea (Time will advance differently outside of your loop).

Further work

  • The code might have bugs. If you find any issues, don't hesitate to fork or open an issue.

  • It might be possible to integrate this code into asyncio.test_utils in some way.

Metadata

Release files for asyncio-time-travel 0.3.0

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

Source distribution (sdist)

Source distribution for asyncio-time-travel 0.3.0
File Size Uploaded Provenance Actions
asyncio_time_travel-0.3.0.tar.gz 8.3 kB Without provenance Details for asyncio_time_travel-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for asyncio-time-travel 0.3.0
File Interpreter ABI Platform Actions
asyncio_time_travel-0.3.0-py3-none-any.whl Python 3 none any Details for asyncio_time_travel-0.3.0-py3-none-any.whl

Total release size: 17.0 kB

Release files / asyncio_time_travel-0.3.0.tar.gz

Download URL asyncio_time_travel-0.3.0.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5989b38b34046e872acf960698295b0b50530d55af861aaf25fce296496f9e23
BLAKE2b-256 checksum
How to use checksums
6472facd0556495b46c5f4e05a629de0db5c9340d0dca882294ef23fb68fab79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / asyncio_time_travel-0.3.0-py3-none-any.whl

Download URL asyncio_time_travel-0.3.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2837e001f23ae0c6106794e0c21ea7e29e2d014143af66fd8376b97fc7d14a22
BLAKE2b-256 checksum
How to use checksums
d51d06a394b82bae2b8e16b7d4b62d440a515e9dfce4f34899efb8fd57894a76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for asyncio-time-travel 0.3.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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