Skip to main content

An easy way to make function run as cron

Project description

pyeasycron

Brief Intro

An easy way to make function run as cron.

Installation

pip3 install pyeasycron

Usage

Use as decorator

  1. use easycron.cron as decorator

    import easycron
    from datetime import datetime
    
    # Expected run when '*/2 * * * *' is satisfied
    @easycron.cron('*/2 * * * *')
    def func1():
        print(f"in func1: {datetime.now()}")
    
    # Expected run when one of ['*/5 8 * * *', '7,14,21 9 * * *'] is satisfied
    @easycron.cron(['*/5 8 * * *', '7,14,21 9 * * *'])
    def func2():
        print(f"in func2: {datetime.now()}")
    
    if __name__ == '__main__':
        easycron.run()
    
  2. use easycron.every as decorator

    import easycron
    from datetime import datetime
    
    # Expected run every 15 minutes
    @easycron.every(minutes=15)
    def func1():
        print(f"in func1: {datetime.now()}")
    
    # Expected run every 4 hours
    @easycron.every(hours=4)
    def func2():
        print(f"in func2: {datetime.now()}")
    
    # Expected run every 2 days
    @easycron.every(days=2)
    def func3():
        print(f"in func3: {datetime.now()}")
    
    if __name__ == '__main__':
        easycron.run()
    
  3. use easycron.every and easycron.cron mixed and stacked

    import easycron
    from datetime import datetime
    
    # Expected run when '*/3 * * * *' is satisfied
    # **OR** when '*/2 * * * *' is satisfied
    # only run **ONCE** when '*/6 * * * *'
    @easycron.cron('*/3 * * * *')
    @easycron.cron('*/2 * * * *')
    def func3():
        print(f"in func3: {datetime.now()}")
    
    
    # Expected run when '*/5 * * * *' is satisfied
    # **OR** every 2 minutes
    # only run **ONCE** when '*/10 * * * *'
    @easycron.every(minutes=2)
    @easycron.cron('*/5 * * * *')
    def func2():
        print(f"in func2: {datetime.now()}")
    
    # Expected run every 5 minutes
    # OR every 7 minutes
    # only run **ONCE** when meeting every 5*7=35 minutes
    @easycron.every(minutes=5)
    @easycron.every(minutes=7)
    def func1():
        print(f"in func1: {datetime.now()}")
    
    if __name__ == '__main__':
        easycron.run()
    

Use with concurrency

As default, easycron.run() run multi functions in serial way
(if several functions are triggered at the same time).

If you want to run in concurrency way, just use concurrency=False parameter.

import easycron
from datetime import datetime

@easycron.cron('*/2 * * * *')
def func1():
    print(f"in func1: {datetime.now()}")

if __name__ == '__main__':
    easycron.run(concurrency=False)

Use without block

As default, easycron.run() blocks currenct process.

If you want to run in unblock way, just use block=False parameter.

import easycron
from datetime import datetime

@easycron.cron('*/2 * * * *')
def func1():
    print(f"in func1: {datetime.now()}")

if __name__ == '__main__':
    easycron.run(block=False)

    # do other things
    ...

Use register and cancel in a common way

import easycron
from datetime import timedelta

def func1():
    print(f"in func1: {datetime.now()}")

def func2():
    print(f"in func2: {datetime.now()}")

if __name__ == '__main__':
    easycron.register(func1, interval=timedelta(minutes=3))
    easycron.register(func2, cron_expr='*/2 * * * *')

    easycron.run(block=False)
    # do other things
    ...

    easycron.cancel(func2)
    # do other things
    ...

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

pyeasycron-0.2.0.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file pyeasycron-0.2.0.tar.gz.

File metadata

  • Download URL: pyeasycron-0.2.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for pyeasycron-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c931db60952d7d3697577862dec9c22eea3c0197e52df163815f7bd8a97990d3
MD5 1e398ed99a65946f036319a21ff8a624
BLAKE2b-256 c22f475ddf99dd58b3e58b27fdbc21790506f687e16f724ef7bcdd939e60af1a

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