umacajadada


Nameumacajadada JSON
Version 0.11 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/umacajadada
Summaryreads a file asynchronously during its execution while somewhere some other process writes to the same file
upload_time2023-09-10 17:42:33
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords read write same time
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# 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



```python

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)

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/umacajadada",
    "name": "umacajadada",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "read,write,same,time",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/7a/4b/71af25024131d50448489925e8e049ef165b2eefb90a18312938f549047b/umacajadada-0.11.tar.gz",
    "platform": null,
    "description": "\r\n# reads a file asynchronously during its execution while somewhere some other process writes to the same file\r\n\r\n## Tested against Windows 10 / Python 3.10 / Anaconda - should work everywhere - pure Python\r\n\r\n## pip install umacajadada\r\n\r\n\r\n\r\n```python\r\n\r\nread_async(\r\n    file: str,\r\n    asthread: bool = True,\r\n    mode: str = \"r\",\r\n    action=lambda line: sys.stderr.write((str(line) + \"\\n\")),\r\n    stoptrigger: list | tuple = (False,),\r\n):\r\n\r\nThis function `read_async` reads a file asynchronously during its execution while somewhere some other\r\nprocess writes to the same file.\r\n\r\nArgs:\r\n\tfile (str): The path to the file to be read.\r\n\tasthread (bool, optional): If True, the function will start as a thread. Default is True.\r\n\tmode (str, optional): The mode in which the file is opened. Default is 'r' (read mode).\r\n\taction (function, optional): A function to be applied to each line of the file. Default is to write the line to stderr.\r\n\tstoptrigger (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].\r\n\tencoding (str, optional): The character encoding of the file. Default is 'utf-8'.\r\n\terrors (str, optional): How to handle encoding errors. Default is 'backslashreplace'.\r\n\r\nReturns:\r\n\tkthread.KThread or None: If as_thread is True, it returns a KThread object. Otherwise, it returns None.\r\n\r\nThe advantage of this function is that it allows for real-time processing of the file while it is being written by another process.\r\nThis 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.\r\n\r\nExample usage:\r\n\tfrom threading import Timer\r\n\tfrom time import time\r\n\tnewfile = f'c:\\\\testfilex{str(time()).replace(\".\",\"_\")}.txt'\r\n\tstoptrigger = [False,]\r\n\tt = read_async(\r\n\t\tfile=newfile,\r\n\t\tas_thread=True,\r\n\t\tmode=\"r\",\r\n\t\taction=lambda line: sys.stderr.write((str(line) + \"\\n\")),\r\n\t\tstoptrigger=stoptrigger,\r\n\t)\r\n\tTimer(5, lambda: stoptrigger.append(True)).start() # Stops after 5 seconds and doesn't print anymore, but os.system goes on.\r\n\tos.system(\"ping 8.8.8.8 -n 10 > \" + newfile + \"\")\r\n\tif not asthread:\r\n\t\treturn _read_file_async(file, mode=mode, action=action)\r\n\telse:\r\n\t\treturn _start_as_tread(file, mode, action, stoptrigger)\r\n\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "reads a file asynchronously during its execution while somewhere some other process writes to the same file",
    "version": "0.11",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/umacajadada"
    },
    "split_keywords": [
        "read",
        "write",
        "same",
        "time"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "594f23c22511cdeacb8319d34e4959232b685649c0bd64cbd50fd38876031f1f",
                "md5": "3a3dafe56274afd103ed5752d608aca8",
                "sha256": "bf040cd0b28c29905a559145deceee60dcab7aafe17b8beb68690d948d9cad89"
            },
            "downloads": -1,
            "filename": "umacajadada-0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3a3dafe56274afd103ed5752d608aca8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7036,
            "upload_time": "2023-09-10T17:42:31",
            "upload_time_iso_8601": "2023-09-10T17:42:31.150967Z",
            "url": "https://files.pythonhosted.org/packages/59/4f/23c22511cdeacb8319d34e4959232b685649c0bd64cbd50fd38876031f1f/umacajadada-0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7a4b71af25024131d50448489925e8e049ef165b2eefb90a18312938f549047b",
                "md5": "eee4a8bbc23e1de180f6f67eb988b1d7",
                "sha256": "f444dee13fcf913ca9459599f9689a739b671543cfccc9054067ffc0b79ca7b2"
            },
            "downloads": -1,
            "filename": "umacajadada-0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "eee4a8bbc23e1de180f6f67eb988b1d7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4987,
            "upload_time": "2023-09-10T17:42:33",
            "upload_time_iso_8601": "2023-09-10T17:42:33.005567Z",
            "url": "https://files.pythonhosted.org/packages/7a/4b/71af25024131d50448489925e8e049ef165b2eefb90a18312938f549047b/umacajadada-0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-10 17:42:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "umacajadada",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "umacajadada"
}
        
Elapsed time: 0.15307s