Skip to main content

Facilities for shared access to files.

Latest release 20230212: Remove dependency on cs.logutils.

Function lockfile(path, ext=None, poll_interval=None, timeout=None)

A context manager which takes and holds a lock file.

Parameters:

  • path: the base associated with the lock file.
  • ext: the extension to the base used to construct the lock file name. Default: ".lock"
  • timeout: maximum time to wait before failing, default None (wait forever).
  • poll_interval: polling frequency when timeout is not 0.

Class SharedAppendFile

A base class to share a modifiable file between multiple users.

The use case was driven from the shared CSV files used by cs.nodedb.csvdb.Backend_CSVFile, where multiple users can read from a common CSV file, and coordinate updates with a lock file.

This presents the following interfaces:

  • __iter__: yields data chunks from the underlying file up to EOF; it blocks no more than reading from the file does. Note that multiple iterators share the same read pointer.

  • open: a context manager returning a writable file for writing updates to the file; it blocks reads from this instance (though not, of course, by other users of the file) and arranges that users of __iter__ do not receive their own written data, thus arranging that __iter__ returns only foreign file updates.

Subclasses would normally override __iter__ to parse the received data into their natural records.

Method SharedAppendFile.__init__(self, pathname, read_only=False, write_only=False, binary=False, newline=None, lock_ext=None, lock_timeout=None, poll_interval=None): Initialise this SharedAppendFile.

Parameters:

  • pathname: the pathname of the file to open.
  • read_only: set to true if we will not write updates.
  • write_only: set to true if we will not read updates.
  • binary: if the file is to be opened in binary mode, otherwise text mode.
  • 'newline: passed to open()`
  • lock_ext: lock file extension.
  • lock_timeout: maxmimum time to wait for obtaining the lock file.
  • poll_interval: poll time when taking a lock file, default DEFAULT_POLL_INTERVAL

Class SharedAppendLines(SharedAppendFile)

A line oriented subclass of SharedAppendFile.

Class SharedCSVFile(SharedAppendLines, SharedAppendFile)

Shared access to a CSV file in UTF-8 encoding.

Class SharedWriteable

Wrapper for a writable file with supported mutex based cooperation.

This is mostly a proxy for the wrapped file exceptthat all .write calls are serialised and when used as a context manager other writers are blocked.

This is to support shared use of an output stream where certain outputs should be contiguous, such as a standard error stream used to maintain a status line or multiline messages.

Release Log

Release 20230212: Remove dependency on cs.logutils.

Release 20211208: Update import.

Release 20201228: New SharedWriteable class to manage concurrent output with a mutexed .write method and a context manager for grouping larger uses.

Release 20190102: Context manager bugfix.

Release 20170608:

  • Facilities for shared files, split out from cs.fileutils.
  • SharedAppend* classes. lockfile function.

Release files for cs.sharedfile 20230212

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

Source distribution (sdist)

Source distribution for cs.sharedfile 20230212
File Size Uploaded
cs.sharedfile-20230212.tar.gz 7.3 kB Details

Built distribution (wheel)

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

Total release size: 14.5 kB

Release files / cs.sharedfile-20230212.tar.gz

Download URL cs.sharedfile-20230212.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1e96db56715baf025b046e8a7df7eb5d3fa186ded7d963fadeeb7d71a8c2b8e0
BLAKE2b-256 checksum
How to use checksums
117777753a753cd7b4eadec92901da92c0e0101279e206ede1f4bfb9ebefec92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / cs.sharedfile-20230212-py3-none-any.whl

Download URL cs.sharedfile-20230212-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d7860f86ce83a933b029453a750540c0322049c5fa403a624e681a75c01e792b
BLAKE2b-256 checksum
How to use checksums
2c1968db33742be8b4f032e1ba504ec61ff6789841423880196a65b8eedf5572
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release history Release notifications | RSS feed

This release

20230212 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