Skip to main content

WhatAmIThinking-aiotools

asyncio tools

Table of Contents

Shields

Problems

The built-in asyncio.shield() function has the following issues:

  • if the current task is cancelled, execution flow will continue but the shielded task will keep running in the background, making program flow difficult to debug and buggy in cases where resources needed by that task are destroyed after that task completes, creating a sort of race condition.
    • the solution of wrapping the task you are running within a task works but is inefficient and slow because you have to wait for a cycle of the loop before you can get your result back
  • code has to be awkwardly chopped up into blocks, because some parts may need to be shielded while others should not be.
    • there is no solution to this using the stdlib alone, except to break into multiple functions which is awkward and hard to follow

Solutions

Use shield_scope() context manager to shield blocks of code from execution The task cancellation is delayed until the scope exits, after which asyncio.CancelledError will be raised on the next await

Use asyncio.get_event_loop().set_task_factory(aiotools.create_shieldable_task) to enable shielding to work properly. This works by creating a custom python asyncio task class.

Locks

Problems

  • Cannot re-enter asyncio.Lock
    • code has to instead be awkwardly chopped up to avoid re-entering the same lock and getting deadlocked. this again is hard to follow.

Solutions

Use RLock which is just a subclass of asyncio.Lock with support for re-entrance

Metadata

Release files for whatamithinking-aiotools 1.0.2

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

Source distribution (sdist)

Source distribution for whatamithinking-aiotools 1.0.2
File Size Uploaded
whatamithinking_aiotools-1.0.2.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for whatamithinking-aiotools 1.0.2
File Interpreter ABI Platform
whatamithinking_aiotools-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / whatamithinking_aiotools-1.0.2.tar.gz

Download URL whatamithinking_aiotools-1.0.2.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e74acda1750e6549239ff6e807986d735fc8f1779532084d41b7ce272e96f25f
BLAKE2b-256 checksum
How to use checksums
7ad0241eb4cfca1bea5f0d7d57ae06e200a1befd35db46f7cf86be469b1b1000
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / whatamithinking_aiotools-1.0.2-py3-none-any.whl

Download URL whatamithinking_aiotools-1.0.2-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
697ae0a1461ed74d6e427aabc5035868ed8e3a0a4d2b8aafcd214fd8b8004c5e
BLAKE2b-256 checksum
How to use checksums
9838346639d79f5b28b3a969a1c43b911e2fb2650b78e2f99578a8304a5f4baa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.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