Skip to main content

Codemod to convert synchronous Python code to async.

Project description

asyncify-codemod

asyncify-codemod is a codemod that converts synchronous Python code into async where possible. It focuses on a few common patterns (like requests, time.sleep, and file I/O) and rewrites them to async equivalents.

Usage

Run with pipx

pipx run asyncify-codemod [files you wish to transform ...]

E.g. if your python source files are located in src/:

pipx run asyncify-codemod src/**/*.py
pipx run asyncify-codemod --stdout path/to/file.py path/to/another.py

Comparison: asyncio.to_thread() vs async rewrites

Because “real async I/O” and “sync I/O in a thread” solve different problems, and they behave very differently at scale and under cancellation.

Where to_thread is great

Fast migration / compatibility shim: you can call existing blocking code from an async app without freezing the event loop. No behavioral rewrite: minimal code churn; good for legacy code you don’t want to touch yet. Sync-only dependencies: if there is no solid async alternative, threads are the pragmatic choice.

Where to_thread breaks down (and why a codemod helps)

  • Concurrency limits: to_thread is bounded by a thread pool. You can run thousands of concurrent await httpx/... calls; you generally can’t run thousands of threads. Under load, this becomes queueing/latency rather than throughput.
  • Cancellation semantics: cancelling an await to_thread(...) usually does not stop the underlying blocking call; it just stops waiting for it. With real async libs, cancellation is often propagated more cleanly (still not perfect everywhere, but materially better).
  • Resource efficiency: threads cost memory + scheduling overhead; async sockets/file ops can be much lighter per in-flight operation. You can’t “mix in” async behavior inside: if a function is wrapped, everything inside it stays sync. A codemod can turn the internals into await points (time.sleep → anyio.sleep, requests → httpx, open() → anyio.open_file), enabling cooperative scheduling throughout the call graph.
  • Observability/context: thread offloading can complicate tracing/log context propagation and debugging (it’s doable, just another layer). Pure async tends to integrate more directly with async frameworks’ middleware/instrumentation.

Use a codemod when you want the codebase to become natively async so it scales better, cancels better, and composes naturally with async frameworks—without hand-editing hundreds of mechanical changes.

Caveats

Sync-only dependencies

Currently, this codemod only rewrites commonly used I/O-bound operations with async alternatives (as mentioned above). If you're using custom libraries where there are no solid async alternatives, they will in your new async functions after a codemod. This is a huge performance blocker and the only way to resolve this right now is to manually update its usages (i.e. send it to asyncio.to_thread).

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

asyncify_codemod-0.2.0.tar.gz (31.2 kB view details)

Uploaded Source

Built Distribution

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

asyncify_codemod-0.2.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file asyncify_codemod-0.2.0.tar.gz.

File metadata

  • Download URL: asyncify_codemod-0.2.0.tar.gz
  • Upload date:
  • Size: 31.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for asyncify_codemod-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c6df5edee72fdf7dad2b150c38f3fc2e7fa1b247b7a2441e1376a8395581d1b8
MD5 687127ef75da855d2ecdf343fe25f0ae
BLAKE2b-256 f6932b66fb39b2e65f02a23c274e91dd21d2e4ab7ece0df91392159d7b418c80

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncify_codemod-0.2.0.tar.gz:

Publisher: publish.yml on 7CaD/python-asyncify-codemod

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file asyncify_codemod-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncify_codemod-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8dd61fbeab0bab8ce3874726746228c580dd3d2c82d8b87edc2572d659d71fab
MD5 8415a0238bb441813aa9e35cc18fa12e
BLAKE2b-256 a97c4fce8e8e777637c7fcf8604308390e3b29bf96da8d24d2438fc3dd92e1c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncify_codemod-0.2.0-py3-none-any.whl:

Publisher: publish.yml on 7CaD/python-asyncify-codemod

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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