Skip to main content

reads a file asynchronously during its execution while somewhere some other process writes to the same file

Tested against Windows 10 / Python 3.10 / Anaconda - should work everywhere - pure Python

pip install umacajadada

read_async(
    file: str,
    asthread: bool = True,
    mode: str = "r",
    action=lambda line: sys.stderr.write((str(line) + "\n")),
    stoptrigger: list | tuple = (False,),
):

This function `read_async` reads a file asynchronously during its execution while somewhere some other
process writes to the same file.

Args:
	file (str): The path to the file to be read.
	asthread (bool, optional): If True, the function will start as a thread. Default is True.
	mode (str, optional): The mode in which the file is opened. Default is 'r' (read mode).
	action (function, optional): A function to be applied to each line of the file. Default is to write the line to stderr.
	stoptrigger (list | tuple, optional): A list or tuple (preferably a list) that triggers the function to stop reading when its last element is True. Default is [False].
	encoding (str, optional): The character encoding of the file. Default is 'utf-8'.
	errors (str, optional): How to handle encoding errors. Default is 'backslashreplace'.

Returns:
	kthread.KThread or None: If as_thread is True, it returns a KThread object. Otherwise, it returns None.

The advantage of this function is that it allows for real-time processing of the file while it is being written by another process.
This is particularly useful in scenarios where the file is continuously updated, and the updates need to be processed immediately, such as in log monitoring or real-time data analysis.

Example usage:
	from threading import Timer
	from time import time
	newfile = f'c:\\testfilex{str(time()).replace(".","_")}.txt'
	stoptrigger = [False,]
	t = read_async(
		file=newfile,
		as_thread=True,
		mode="r",
		action=lambda line: sys.stderr.write((str(line) + "\n")),
		stoptrigger=stoptrigger,
	)
	Timer(5, lambda: stoptrigger.append(True)).start() # Stops after 5 seconds and doesn't print anymore, but os.system goes on.
	os.system("ping 8.8.8.8 -n 10 > " + newfile + "")
	if not asthread:
		return _read_file_async(file, mode=mode, action=action)
	else:
		return _start_as_tread(file, mode, action, stoptrigger)

Metadata

Release files for umacajadada 0.11

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

Source distribution (sdist)

Source distribution for umacajadada 0.11
File Size Uploaded Provenance Actions
umacajadada-0.11.tar.gz 5.0 kB Without provenance Details for umacajadada-0.11.tar.gz

Built distribution (wheel)

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

Total release size: 12.0 kB

Release files / umacajadada-0.11.tar.gz

Download URL umacajadada-0.11.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f444dee13fcf913ca9459599f9689a739b671543cfccc9054067ffc0b79ca7b2
BLAKE2b-256 checksum
How to use checksums
7a4b71af25024131d50448489925e8e049ef165b2eefb90a18312938f549047b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / umacajadada-0.11-py3-none-any.whl

Download URL umacajadada-0.11-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bf040cd0b28c29905a559145deceee60dcab7aafe17b8beb68690d948d9cad89
BLAKE2b-256 checksum
How to use checksums
594f23c22511cdeacb8319d34e4959232b685649c0bd64cbd50fd38876031f1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Security for umacajadada 0.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.11 This release

2 release files No provenance

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