Skip to main content

asyncio tools

Project description

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

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

whatamithinking_aiotools-1.0.2.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

whatamithinking_aiotools-1.0.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file whatamithinking_aiotools-1.0.2.tar.gz.

File metadata

File hashes

Hashes for whatamithinking_aiotools-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e74acda1750e6549239ff6e807986d735fc8f1779532084d41b7ce272e96f25f
MD5 e191696e3e0da62f37c91b754c2a1d20
BLAKE2b-256 7ad0241eb4cfca1bea5f0d7d57ae06e200a1befd35db46f7cf86be469b1b1000

See more details on using hashes here.

File details

Details for the file whatamithinking_aiotools-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for whatamithinking_aiotools-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 697ae0a1461ed74d6e427aabc5035868ed8e3a0a4d2b8aafcd214fd8b8004c5e
MD5 01c4a444571b4c4cc306469f7eafabf3
BLAKE2b-256 9838346639d79f5b28b3a969a1c43b911e2fb2650b78e2f99578a8304a5f4baa

See more details on using hashes here.

Supported by

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