Skip to main content

Utility for initialization that runs an extra function once before a function is called

Project description

English | 简体中文

init_once

Introduction

init_once is a thread-safe decorator that runs an initialization function once right before the decorated function is called for the first time. The initialization runs only once. No outer flag needed.

Dependencies and Version Requirements

  • Python 3.8 or higher

Usage

Basic Syntax

@init_once(initializer, *init_args, **init_kwds)
def func(...):
    ...
  • initializer: The initialization function that needs to be executed once.
  • *init_args: Positional arguments to pass to initializer.
  • **init_kwds: Keyword arguments to pass to initializer.

Simple Example

from init_once import init_once

# Initialization function: runs only once
def init_func(msg: str):
    print(f"Initializing msg: {msg}")

@init_once(init_func, "hello")
def main_task(data: str):
    print(f"Processing: {data}")

# First call: init_func executes first, then main_task
main_task("first")
# Output:
# Initializing msg: hello
# Processing: first

# Subsequent calls: only main_task executes
main_task("second")
# Output:
# Processing: second

# The above is equivalent to:
initialized = False

def init_func(msg: str):
    print(f"Initializing msg: {msg}")

def main_task(data: str):
    global initialized
    if not initialized:
        init_func("hello")
        initialized = True

    print(f"Processing: {data}")

main_task("first")
main_task("second")

Notes

  1. This decorator is only for simple initialization (return ignored). If you need the return value, you should handle the initialization explicitly instead of using this decorator.
  2. Async version is to be implemented in the future.

License

This project is licensed under the MIT License. See the LICENSE file for details.


If you have any questions or suggestions for improvement, feel free to open an Issue or submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

init_once-0.1.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

init_once-0.1.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file init_once-0.1.2.tar.gz.

File metadata

  • Download URL: init_once-0.1.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for init_once-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bd7c401ea3ebe591e61572c127d2c394c911ac23ebc992a10a0c6646a0123ff0
MD5 9d4f53a4d6a5405a75a3eb84083992d9
BLAKE2b-256 7566607da3c74f5bbc06474b85688f6e435985f3a4704f57a9999cd49d630ea2

See more details on using hashes here.

File details

Details for the file init_once-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: init_once-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for init_once-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 55d7b5741172827603e33cfce44658ef313d55757f84e0e56996431e3d93cc73
MD5 85341e9d86b326aa2456b790c0678b56
BLAKE2b-256 de4b4b8089a0c24ddf50a759ff281c44e746443dfbab43316c90064914aa8120

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page