The missing Python async timer.
Project description
Async timer
This package provides an async timer object, that should have been part of batteries.
Purpose
Sometimes, you need a way to make something happen over and over again at certain times, like updating information or sending reminders. That's where Async Timer comes in. It lets you set up these repeated actions easily.
This package is particularly useful for tasks like automatically updating caches in the background without disrupting the primary application's workflow.
Features
- Zero Dependencies: Written entirely in Python, Async Timer operates independently without needing any external libraries.
- Versatility in Callables: It accommodates various callable types, including:
- Synchronous functions
- Asynchronous functions
- Synchronous generators
- Asynchronous generators
- Wait for the Next Tick: You can set it up so your program waits for the timer to do its thing, and then continues.
- Keep Getting Updates: You can use it in a loop to keep getting updates every time the timer goes off.
- Cancel anytime: The timer object can be stopped at any time either explicitly by calling
stop()/cancel()method OR it can stop automatically on an awaitable resolving (thecancel_awsconstructor artument) - Test friendly: The package provides an additional
mock_async_timer.MockTimerclass with mocked sleep function to aid in your testing
Example Usage
FastAPI
This snippet starts fastapi webserver with the refresh_db function being executed every 5 seconds, refresing a shared DB_CACHE object.
import contextlib
import time
import uvicorn
from fastapi import FastAPI
import async_timer
DB_CACHE = {"initialised": False}
async def refresh_db():
global DB_CACHE
DB_CACHE |= {"initialised": True, "cur_value": time.time()}
@contextlib.asynccontextmanager
async def lifespan(_app: FastAPI):
async with async_timer.Timer(delay=5, target=refresh_db) as timer:
await timer.wait(hit_count=1) # block until the timer triggers at least once
yield
app = FastAPI(lifespan=lifespan)
@app.get("/")
async def root():
return {"message": "Hello World", "db_cache": DB_CACHE}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
join()
import async_timer
timer = async_timer.Timer(12, target=lambda: 42)
timer.start()
val = await timer.join() # `val` will be set to 42 after 12 seconds
for loop
# Async for loop example
import async_timer
import time
with async_timer.Timer(14, target=time.time) as timer:
async for time_rv in timer:
print(f"{time_rv=}") # Prints current time every 14 seconds
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file async_timer-1.1.6.tar.gz.
File metadata
- Download URL: async_timer-1.1.6.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0a07574b9ccc9ac484ec178deb4f1018150cb96f007cc12ee2a96280252c310
|
|
| MD5 |
8e42c0803944805e28ace35f629588d5
|
|
| BLAKE2b-256 |
afeefe0f95b436c64873795e89f0d13cbf0f4c88afae00b3ebba46363d11c13f
|
File details
Details for the file async_timer-1.1.6-py3-none-any.whl.
File metadata
- Download URL: async_timer-1.1.6-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b178e0ecbcf3c5c9711d650c90a04b8691d09bb2145c123c053d382927b7e2b8
|
|
| MD5 |
da61fe56390b952b0b0e47d476657940
|
|
| BLAKE2b-256 |
08eb21b78921bb57c690fbabc42ff3f7220f6f7310219b83311f5ebbe4462a8a
|