Skip to main content

asyncio tools

Project description


header-includes: - \usepackage{enumitem} - \setlistdepth{20} - \renewlist{itemize}{itemize}{20} - \renewlist{enumerate}{enumerate}{20} - \setlist[itemize]{label=$\cdot$} - \setlist[itemize,1]{label=\textbullet} - \setlist[itemize,2]{label=--} - \setlist[itemize,3]{label=*} - \usepackage{fvextra} - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\{}}

output: rmarkdown::pdf_document: keep_tex: yes

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.1.tar.gz (9.5 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.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for whatamithinking_aiotools-1.0.1.tar.gz
Algorithm Hash digest
SHA256 14fcd96f7db604df03a27a12bbc581b8d0314afb1b1026fb06e8a1510a1561e4
MD5 ee7b19f6bbb3160c7b7934b699c6f54d
BLAKE2b-256 1ed20c4df46d3b4d6d66f6c8d725d98fea100a821ad10d0627a24518925e8965

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for whatamithinking_aiotools-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 39ba67cab624a066cef5565a0092f9990f61c9c388ce632e213678c5bced922b
MD5 5695f5db40e3dd27265f6a6c088b4cb3
BLAKE2b-256 20b3e406e13468c76be3ba122473390fc8093539c70a1ca6833d84b056a19613

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