Skip to main content

A simple and up-to-date module that enables compatibility with asyncio and fileIO operations in Python.

Purpose

Operating on files in Python is a synchronous activity. When normally operating on file this does not cause any problems. However, when files are tampered with in code that also runs the asyncio event loop, this can cause the asyncio event loop to block. This negatively effects the program’s performance and should therefore be avoided. The asyncfile module avoids this problem by running the file operations in a separate thread so that the event loop is not as harshly affected.

Features

  • async and await syntax is used

  • There is almost complete coverage of the io module’s classes

  • The syntax and utility is similar to the built-in Python open function and the built-in io module

  • Lightweight in speed and memory

Installation

Installing asyncfile should be done through PIP:

pip install asyncfile

Open Examples

If you were to have regular, blocking code, you can easily transition it to asyncfile

Blocking:

with open('fake_file', 'r') as f:
    print(f.read())

Non-Blocking:

import asyncfile
import asyncio

custom_loop = asyncio.get_event_loop() # You can pass in your own loop

async def open_file():
    async with asyncfile.open('fake_file', 'r', loop=custom_loop) as f:
        print(await f.read())

custom_loop.run_until_complete(open_file())

These both produce the same results, but one is better suited for asyncio-based code.

IO Examples

Blocking:

import io

wrap = io.FileIO('fake_file.txt', 'wb')

buff = io.BufferedReader(wrap)
buff.write(b'Random bytes')
print(buff.fileno())
print(buff.raw)
print(buff.readable())
buff.close()

Non-Blocking:

import asyncfile
import asyncio

async def no_block(file_path):
    wrap = asyncfile.AsyncFileIO(file_path, 'rb')
    buff = asyncfile.AsyncBufferedReader(wrap)
    await buff.read(-1)
    print(await buff.fileno())
    print(buff.raw)
    print(await buff.readable())
    await buff.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(no_block('fake_file.txt'))

Blocking:

import io

for i in io.FileIO('fake_file.txt'):
    print(i)

Non-Blocking:

import asyncfile
import asyncio

async def async_iteration():
    async for i in asyncfile.AsyncFileIO('fake_file.txt'):
        print(i)

Release files for asyncfile 1.0.0

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

Source distribution (sdist)

Source distribution for asyncfile 1.0.0
File Size Uploaded
asyncfile-1.0.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncfile 1.0.0
File Interpreter ABI Platform
asyncfile-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.2 kB

Release files / asyncfile-1.0.0.tar.gz

Download URL asyncfile-1.0.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bbf9974703c837a0ffd09905f455854958de32284dbafa98c2b7f7e577061ebf
BLAKE2b-256 checksum
How to use checksums
aaf8967fd7ded17a7514544ffbd52ac771695c34544cd53abf5d014423029478
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

Release files / asyncfile-1.0.0-py3-none-any.whl

Download URL asyncfile-1.0.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
96a21047d9c522418441da270843e50b5eee5a5c32982cbd24e476ba503a9e7f
BLAKE2b-256 checksum
How to use checksums
532927c9d7850c104126486de461a9fc8ca84db6bc11bb78c5383d983c7b640f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

Release history Release notifications | RSS feed

This release

1.0.0 This release

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