Skip to main content

a Python library to schedule and persist async function calls, ensuring they run exactly when you need them, even after restarts.

Project description

asyncpause

Welcome to asyncpause! This nifty little Python library is here to make sure your async functions get called exactly when you want them to, even if your program decides to take a little nap in between. With asyncpause, you can schedule async function calls to run at specific times or after certain delays, and it will ensure they are executed even after a restart. Say goodbye to missed calls and hello to punctuality!

Features

  • Schedule async function calls with ease.
  • Save call details to disk to survive restarts.
  • Load and execute scheduled calls automatically.
  • Supports delays in seconds, timedeltas, or specific datetime objects.

Installation

You can install asyncpause via pip:

pip install asyncpause

Usage

Here's a quick example to get you started. Let's schedule some functions and watch the magic happen.

import asyncio
from datetime import datetime, timedelta
from asyncpause import AsyncPause

async def main():
    async def aprint(*args, **kwargs):
        print(*args, **kwargs)

    d = await AsyncPause.setup(aprint)
    # you can also pass a filepath to save the scheduled calls
    # or in_memory=True to keep them in memory :)
    d.set(datetime.now(), "Hello, world!")
    d.set(timedelta(seconds=5), "Hello, world! 5 sec spent!")
    d.set(timedelta(seconds=1), "Hello, world after a second!")
    d.set(timedelta(seconds=2))
    await asyncio.sleep(6)

asyncio.run(main())

In this example, the aprint function its just an example. Just run this script, and watch your console get filled with timely greetings.

If you want to decouple the function call from background processes initialiation, you can use the start_background method:

import asyncio
from datetime import datetime, timedelta
from asyncpause import AsyncPause

async def aprint(*args, **kwargs):
    print(*args, **kwargs)

D = AsyncPause(aprint, filepath="aprint.scheduled.calls")

async def main():
    d = await D.start_background()
    d.set(datetime.now(), "Hello, world!")
    d.set(timedelta(seconds=1), "Hello, world after a second!")
    await asyncio.sleep(3)
asyncio.run(main())

How It Works

  1. Initialization: Create an AsyncPause object with your target function.
  2. Scheduling: Use the set method to schedule your function calls. You can specify a delay in seconds, a timedelta, or a specific datetime.
  3. Persistence: The library saves the scheduled calls to a file (or keeps them in memory if you prefer).
  4. Execution: It runs the scheduled calls at the right time, even after restarts.

Why asyncpause?

Because sometimes you just need your async functions to be fashionably late, not never! This library handles all the tedious work of saving, loading, and running your scheduled functions, so you can focus on writing great code without worrying about time.

Contributing

Feel free to fork this repository, make some changes, and submit a pull request. Whether it's fixing bugs, adding features, or just improving documentation, all contributions are welcome!

License

This project is licensed under The Unlicense.

Conclusion

asyncpause is your go-to tool for scheduling async function calls in Python. It's reliable, easy to use, and ensures that your functions are called when they should be, no matter what. So why wait? Install asyncpause today and give your async functions the punctuality they deserve!


P.S. Why was the async function always on time? Because it knew how to await its turn!

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

asyncpause-0.1.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

asyncpause-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file asyncpause-0.1.1.tar.gz.

File metadata

  • Download URL: asyncpause-0.1.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for asyncpause-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0c688bc834c58ebf976a1e8f74ac8167640b8c57631794a4aeeac46d7798225c
MD5 d2e1976f34beb4e8701c55f14ba23ae0
BLAKE2b-256 73636c43164ed5794d7366a5225d07abd32bc1d07311d36cca3c2c14f652ae61

See more details on using hashes here.

File details

Details for the file asyncpause-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: asyncpause-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for asyncpause-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 579f8e1ead0356039f2d1d6c0ac73a25cf5ff7a5c5652706f3dc1559fd0f439e
MD5 00d9f5e083e1cb6cb4c462d5e81a8a3c
BLAKE2b-256 41f2c6f8f1b7cb37442dd398002f248d424e8ad449301f6a841dce3883c6c15f

See more details on using hashes here.

Supported by

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