Skip to main content

A tiny library for monitoring and testing asyncio programs

Project description

This is a tiny library for monitoring and testing asyncio programs. Its monitoring features are meant to be always on in production.

Installation

aiodebug is only compatible with Python 3.8 and higher. There are no plans to support older versions.

aiodebug is available on PyPI and you can install it with:

pip install aiodebug

or

poetry add aiodebug

aiodebug will use logwood if it is installed, otherwise it will default to the standard logging module.

Log warnings when callbacks block the event loop

import aiodebug.log_slow_callbacks

aiodebug.log_slow_callbacks.enable(0.05)

This will produce WARNING-level logs such as

Executing <Task pending coro=<foo() running at /home/.../foo.py:37>
wait_for=<Future pending cb=[Task._wakeup()]>> took 0.069 seconds

asyncio already does this in debug mode, but you probably don’t want to enable full-on debug mode in production.

Track event loop lags in StatsD

import aiodebug.monitor_loop_lag

aiodebug.monitor_loop_lag.enable(statsd_client)

Tracks how much scheduled calls get delayed and sends the lags to StatsD.

loop-lags.png

Dump stack traces of all threads if the event loop hangs for too long

import aiodebug.hang_inspection

dumper = aiodebug.hang_inspection.start('/path/to/output/directory', interval = 0.25)  # 0.25 is the default
...
await aiodebug.hang_inspection.stop_wait(dumper)

Enabling this function may help you in case one of your threads (sometimes) runs a CPU-bound operation that completely stalls the event loop, but you don’t know which thread it is or what it is doing.

Every time the event loop hangs (doesn’t run a scheduled ‘monitoring’ task) for longer than the given interval, aiodebug will create 3 stack traces, 1 second apart, in your output directory. For example:

-rw-r--r--  1 user  group   6.7K  4 Jan 09:41 stacktrace-20220104-094154.197418-0.txt
-rw-r--r--  1 user  group   7.0K  4 Jan 09:41 stacktrace-20220104-094155.206574-1.txt
-rw-r--r--  1 user  group   6.6K  4 Jan 09:41 stacktrace-20220104-094156.211781-2.txt

Each file then contains the Python stack traces of all threads that were running or waiting at the time. You might be able to find your culprit blocking the event loop at the end of one of the traces.

Speed up or slow down time in the event loop

This is mainly useful for testing.

import aiodebug.testing.time_dilated_loop

loop = aiodebug.testing.time_dilated_loop.TimeDilatedLoop()
asyncio.set_event_loop(loop)

loop.time_dilation = 3
await asyncio.sleep(1)  # Takes 0.333s of real time

loop.time_dilation = 0.1
await asyncio.sleep(1)  # Takes 10s of real time

quantlane.png

aiodebug was made by Quantlane, a systematic trading firm. We design, build and run our own stock trading platform.

Project details


Download files

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

Source Distribution

aiodebug-2.2.1.tar.gz (6.9 kB view details)

Uploaded Source

File details

Details for the file aiodebug-2.2.1.tar.gz.

File metadata

  • Download URL: aiodebug-2.2.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2

File hashes

Hashes for aiodebug-2.2.1.tar.gz
Algorithm Hash digest
SHA256 7e4a89309ea1403cbb2d39b27ac5431c39a8936f8338034af9b8416232fbfa6c
MD5 fe6696502a666b950301cc3072f37b0d
BLAKE2b-256 a71857721b44363a7ee2ed78beb67c92fbd2deb9fb5b0e8c92f4bd590fc1893c

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page