tendo

0.4.0 · active · verified Thu Apr 16

Tendo is a Python library that extends core functionality, providing tools such as transparent Unicode support for file operations, a `tee` implementation for concurrent output, and an improved `execfile` function. Its prominent `singleton` module ensures that only a single instance of a Python script or application can run at a time on a given machine, leveraging lock files for process management. As of version 0.4.0, it requires Python 3.8+ and uses `SingleInstance` as a context manager.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `tendo.singleton.SingleInstance` as a context manager to ensure only one instance of a Python script runs at a time. If a second instance attempts to acquire the lock, a `SingleInstanceException` is raised.

import time
import os
from tendo.singleton import SingleInstance, SingleInstanceException

# To demonstrate, use a unique flavor_id for this quickstart example.
# In a real application, you might use a consistent string like 'my_app_name'
# or rely on the default (based on script path) if appropriate.

try:
    # Acquire a single instance lock. If another instance is running, an exception is raised.
    with SingleInstance(flavor_id="my_tendo_quickstart_app"):
        print("Application started. This instance is the only one running.")
        print("Lock file location (typically in temp directory): .tendo-my_tendo_quickstart_app.lock")
        print("Sleeping for 10 seconds. Try running this script again in another terminal to see the SingleInstanceException.")
        time.sleep(10)
        print("Application finished.")
except SingleInstanceException:
    print("Another instance of this application with the same flavor_id is already running.")
    print("Exiting as only one instance is allowed.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →