Snowball(or n+1) function call prevention utilities.
Project description
Snowball
Library with several utilities for merging "n+1" amount function calls into one.
This utilities might be helpful for tasks/functions that should run not very frequently, but there could be a lot of initiating calls to that functions.
Installation
pip install px-snowball
Usage
Simple usage:
from px_snowball import thread_throttle, thread_debounce
# Or shortcuts:
# from px_snowball import throttle, debounce
context = {}
# We're delaying function execution after each call:
@thread_debounce(0.2)
def debouncer(values):
# `values` will be a list of all arguments and key arguments it was called.
for args, kwargs in values:
# do some stuff
print(args)
debouncer(1)
debouncer(2)
debouncer(3)
time.sleep(0.1)
debouncer(4)
time.sleep(0.2)
# And only now `debouncer` function will be called.
# The same is for throttle function.
More practical example.
Let's assume we need to write messages to file. But system calls for every message - is too expensive:
from px_snowball import thread_throttle, thread_debounce
# At most every 1 second messages will be written to log file:
@thread_throttle(1)
def log(values: List[Tuple[Tuple[str], dict]):
messages = (message for (message, *args), kwargs in values)
data_to_write = '\n'.join(messages)
with open('/tmp/log.log', 'a+') as f:
f.write(data_to_write)
log('First')
log('Second')
# Will be empty:
# > cat /tmp/log.log
time.sleep(0.5)
log('Third')
# Still empty:
# > cat /tmp/log.log
time.sleep(0.5)
# Now, after 1 second has passed all logs are in file.
# Write operation was executed only once, instead of 3 times as would be if func
# was executed as usual.
# > cat /tmp/log.log
# > First
# > Second
# > Third
Yes, the resulting function is a bit ugly, but the main profit is that we could perform our operations once in "bulk"(with possible optimizations) even if we call functions 10k times.
Changelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
[Unreleased]
[0.1.0]
Initial version.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file px-snowball-0.1.0.tar.gz
.
File metadata
- Download URL: px-snowball-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 175f3f9cc8b466712199afa384923405f028d8b808a9c9fb0d2b569ede3679c9 |
|
MD5 | bff2734955d7359767c9b7197f70b2fa |
|
BLAKE2b-256 | 75380645eb064e4fc7909e147c8a9578dead6c42e9fb7fccb0779c32ab80f118 |