Skip to main content

Dynamic task scheduler

Project description

ClockParts

Hi There 👋!

I'm the developer of ClockParts. I encountered many challenges when working with scheduling tasks in Python, whether in multi-threading projects, APIs, or microservices. So, I created ClockParts to address these issues. It's simple, compatible with asynchronous operations and threads, and human-readable, making scheduling in Python easier and more efficient.

Installation

It's very easy on PyPI:

pip install ClockParts

Usage

This usage is very simple and based on Shaft and Cog. Shaft is the manager of Cog, and Cog represents your tasks.

Project structure:

├── cogs
│   └── my_cogs.py
├── main.py

Shaft

Now, let's see how to create a Shaft:

# main.py
from ClockParts import Shaft
import asyncio

shaft = Shaft()

if __name__ == "__main__":
    # Create a new Shaft
    shaft = Shaft()
    # Add the "cogs" folder (by default it's "cogs")
    shaft.add_cogs("cogs")
    # Run the Shaft
    asyncio.run(shaft.run())

It's simple! Basically, you need an asynchronous context in your project. If you need an internal explanation, in short, it checks every second if there is a new Cog Task to execute.

Cogs

Let's move on to your Cogs!

# cogs/my_cogs.py

from ClockParts import Cog
from datetime import timedelta


class MyCog(Cog):
    """Examples of Cog usage"""

    @Cog.task(timedelta(seconds=5))
    async def task1(self):
        """
        Task to run every after 5 seconds (using timedelta)
        """
        print("Executing task 1")
    
    @Cog.task("11:00")
    async def task2(self):
        """
        Task to be performed every day at 11:00
        """
        print("Executing task 2")

    @Cog.task("thu 10:00")
    async def task3(self):
        """
        Task to be executed every Thursday at 10:00
        """
        print("Executing task 3")

    @Cog.task("1m 10:00")
    async def task4(self):
        """
        Task to be executed every month at 10:00
        """
        print("Executing task 4")
    
    @Cog.task("mon 10:00")
    async def task5(self):
        """
        Task to be executed every Monday at 10:00
        """
        print("Executing task 5")

Does it look simple? Yes, indeed! In short, you don't need to import ClockParts, just import Cog and use its methods to schedule tasks. There are two ways to schedule your tasks:

  • String format: Ideal for selecting specific days of the week and month.
  • Timedelta format: Ideal for scheduling tasks at specific times. In most cases, you should use timedelta. Your Cogs are ready! To run your Shaft, you just need to execute main.py, and you're good to go!

If you're contributing to the repository, feel free to open an issue, pull request, or fork. You're welcome!

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

clockparts-0.1.56.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

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

clockparts-0.1.56-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file clockparts-0.1.56.tar.gz.

File metadata

  • Download URL: clockparts-0.1.56.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Windows/11

File hashes

Hashes for clockparts-0.1.56.tar.gz
Algorithm Hash digest
SHA256 d6bb9b76b7b7de3443076c90218f64c0d63642322ef8a819cbc4bb4e2c4ab2ad
MD5 9c4cf0af0d128cb04ca70f5459c76768
BLAKE2b-256 11ff868208da9b1e2eb98fb6f8e16c4b63e63797dc7de86e2283cb6a40028486

See more details on using hashes here.

File details

Details for the file clockparts-0.1.56-py3-none-any.whl.

File metadata

  • Download URL: clockparts-0.1.56-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Windows/11

File hashes

Hashes for clockparts-0.1.56-py3-none-any.whl
Algorithm Hash digest
SHA256 716a12f39991a197d097f19254fbfa04612636c1b4b844d1389bf39a777e93e4
MD5 10a3324bd409a5fb06420f7b89eee0c3
BLAKE2b-256 63e39be441f2ab2a26b7e3a33c89c6d045673494b33d85200d4642620f29f6ef

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