|
| 1 | +# async_task_scheduler |
| 2 | + |
| 3 | +`async_task_scheduler` is a Python module that allows you to schedule asynchronous tasks using various scheduling strategies such as cron-like schedules, one-time execution, and more. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install the module, simply install it using `pip`: |
| 8 | + |
| 9 | +```sh |
| 10 | +pip install async_task_scheduler |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Creating a Scheduler |
| 16 | + |
| 17 | +First, create an instance of the `Scheduler` class: |
| 18 | + |
| 19 | +```python |
| 20 | +from async_task_scheduler import Scheduler |
| 21 | + |
| 22 | +scheduler = Scheduler() |
| 23 | +``` |
| 24 | + |
| 25 | +### Adding Tasks |
| 26 | + |
| 27 | +You can add tasks to the scheduler using various decorators: |
| 28 | + |
| 29 | +#### Always |
| 30 | + |
| 31 | +Runs the task every minute. |
| 32 | + |
| 33 | +```python |
| 34 | +@scheduler.always |
| 35 | +async def print_time_every_minute(): |
| 36 | + print("Cron time:", datetime.now().strftime("%M %H %d %m %w")) |
| 37 | +``` |
| 38 | + |
| 39 | +#### Cron |
| 40 | + |
| 41 | +Runs the task based on a cron-like schedule. |
| 42 | + |
| 43 | +```python |
| 44 | +@scheduler.cron("*/2 * * * *") |
| 45 | +async def two_minutes(): |
| 46 | + print("Every 2 minutes") |
| 47 | +``` |
| 48 | + |
| 49 | +#### Hourly |
| 50 | + |
| 51 | +Runs the task at the start of every hour. |
| 52 | + |
| 53 | +```python |
| 54 | +@scheduler.hourly |
| 55 | +async def hourly_task(): |
| 56 | + print("It is now", datetime.now().strftime("%H:%M")) |
| 57 | +``` |
| 58 | + |
| 59 | +#### Daily |
| 60 | + |
| 61 | +Runs the task at the start of every day. |
| 62 | + |
| 63 | +```python |
| 64 | +@scheduler.daily |
| 65 | +async def daily_task(): |
| 66 | + print("Good morning!") |
| 67 | +``` |
| 68 | + |
| 69 | +#### Weekly |
| 70 | + |
| 71 | +Runs the task at the start of every week. |
| 72 | + |
| 73 | +```python |
| 74 | +@scheduler.weekly |
| 75 | +async def weekly_task(): |
| 76 | + print("This is a weekly task!") |
| 77 | +``` |
| 78 | + |
| 79 | +#### Monthly |
| 80 | + |
| 81 | +Runs the task at the start of every month. |
| 82 | + |
| 83 | +```python |
| 84 | +@scheduler.monthly |
| 85 | +async def monthly_task(): |
| 86 | + print("Welcome to", datetime.now().strftime("%B")) |
| 87 | +``` |
| 88 | + |
| 89 | +#### At Start |
| 90 | + |
| 91 | +Runs the task once when the scheduler starts. |
| 92 | + |
| 93 | +```python |
| 94 | +@scheduler.at_start |
| 95 | +async def long_task(): |
| 96 | + print("Long task starting") |
| 97 | + await asyncio.sleep(5) |
| 98 | + print("Long task done") |
| 99 | +``` |
| 100 | + |
| 101 | +#### At Specific Time |
| 102 | + |
| 103 | +Runs the task at a specific datetime. |
| 104 | + |
| 105 | +```python |
| 106 | +@scheduler.at(datetime(2025, 3, 10, 18, 29)) |
| 107 | +async def future_task(): |
| 108 | + print("This is a task for a specific time.") |
| 109 | +``` |
| 110 | + |
| 111 | +### Running the Scheduler |
| 112 | + |
| 113 | +To run the scheduler, await the `run` method or call it using `asyncio.run`: |
| 114 | + |
| 115 | +```python |
| 116 | +await scheduler.run() |
| 117 | +``` |
| 118 | + |
| 119 | +or |
| 120 | + |
| 121 | +```python |
| 122 | +asyncio.run(scheduler.run()) |
| 123 | +``` |
| 124 | + |
| 125 | +The scheduler will run indefinitely until the program is stopped. |
| 126 | + |
| 127 | +## Example |
| 128 | + |
| 129 | +See the end of the source file for a complete example. |
| 130 | + |
| 131 | +## License |
| 132 | + |
| 133 | +This project is licensed under the MIT License. |
0 commit comments