Skip to main content

A library that provides time-based task scheduling using green threads via gevent.

Project description

Travs-CI status Coverage Status

GreenClock is a time-based task scheduler using gevent

With GreenClock, you can: Schedule a task to run every X seconds, daily, weekly, monthly, or at certain times (such as application startup).

GreenClock launches a green thread per task. Therefore every task will be executed in a concurrent manner, without blocking each other.

Status

This module is currently under development.

Features

- A simple to use API for scheduling jobs.
- Lightweight (depending on gevent only)
- Works with Python 2.7+
- Support the following scheduling scenarios:
    + run every X seconds
    + run every hour at specified minute and second
    + run at specified time (hour:minute:second) every day
    + more to come

Installation

This library depends on gevent 1.5

$ pip install cython -e git://github.com/surfly/gevent.git@1.0rc2#egg=gevent

To install GreenClock from pip:

$ pip install greenclock

You can also install it into your Python application directory

$ pip install --install-option="--prefix=/path/to/python/app" greenclock

To install GreenClock from source:

$ git clone git@github.com:pcdinh/greenclock.git
$ python setup.py install

Usage

from greenclock.utils import Scheduler
from datetime import datetime
import time

def func_1():
    print('Calling func_1() at ' + str(datetime.now()))
    time.sleep(2)
    print('Ended call to func_1() at ' + str(datetime.now()))

def func_2():
    print('Calling func_2() at ' + str(datetime.now()))
    time.sleep(2)
    print('Ended call to func_2() at ' + str(datetime.now()))

if __name__ == "__main__":
    scheduler = Scheduler(logger_name='task_scheduler')
    scheduler.schedule('task_1', greenclock.every_second(4), func_1)
    scheduler.schedule('task_2', greenclock.every_second(1), func_2)
    # Run hourly task at 41:00 every day
    scheduler.schedule('task_3', greenclock.every_hour(minute=41, second=0), func_3)
    # Run daily task at 12:35:00
    scheduler.schedule('task_2', greenclock.every_hour(hour=12, minute=35, second=0), func_2)
    # To start the scheduled tasks immediately, specify 'once' for `start_at`
    # Other values:
    # * `next_minute`: Wait until the first seconds of the next minute to run
    # * `next_hour`: Wait until the first seconds of the next hour to run
    # * `tomorrow`: Wait until the first seconds of tomorrow to run
    scheduler.run_forever(start_at='once')

Basically to schedule a periodic task or job, you need to specify the following parameters:

+ Task name: `task_1`
+ A timer that let the scheduler know how to run a periodic task
# run the task for every 4 seconds
from greenclock.utils import every_second, every_hour
every_second(4)
# run the task every day at 01:10:00
every_hour(hour=1, minute=10, second=0)
+ A function or callable object
+ Optional parameters to the above function or callable object
scheduler.schedule('task_1', greenclock.every_second(1), func_1, param1, param2, named_param=2)

Scheduler object can run a separate process which never exits if you want it to

scheduler.run_forever(start_at='once')

Bitdeli Badge githalytics.com alpha

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

greenclock-0.3.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

greenclock-0.3.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file greenclock-0.3.2.tar.gz.

File metadata

  • Download URL: greenclock-0.3.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for greenclock-0.3.2.tar.gz
Algorithm Hash digest
SHA256 96bd942518699a1a44f84f50f4eeb49a256ad6ca4e10503c0cbfe77f7163d3a1
MD5 9be0cdba9ef8b883d913c6e452c11995
BLAKE2b-256 6b4b1da5f11d5748a5385a5568d1fdbeb4f33432659067e4ad2759fef698ae08

See more details on using hashes here.

File details

Details for the file greenclock-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: greenclock-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for greenclock-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 58eb353b9a951a38fb5984b8c407c8c4e9a955189eca6fa7cc9b4fb36a2a828c
MD5 8dfc9dd7efcc5a94ccce55bf880a8715
BLAKE2b-256 218fde233b186fb7302355ca22b1d617cc44a5929b0b48a808c9817e166ec136

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