Skip to main content

Background task management using gevent and green threads.

Project description

gevent-tasks

pypi docs MIT License

Background task manager using Gevent and Green threads.

This library is designed to allow a developer to run arbitrary "tasks" as background threads on a fixed/normalized interval. Each task is a wrapped callable that takes at least one parameter task, a reference to itself. Timings and related metadata can be accessed via the task value as well as the ability to stop and reschedule itself for future events.

TaskManager has a TaskPool that runs Tasks.

Installation

The latest version from pypi,

$ pip install gevent-tasks

The latest development version from source,

$ pip install git+git@github.com:blakev/gevent-tasks.git@develop

Examples

A classic example,

# print our system uptime every minute, indefinitely

from datetime import timedelta
from gevent_tasks import TaskManager, cron

manage = TaskManager()

@manage.task(interval=cron('* * * * *'))
def system_uptime(task):
    with open('/proc/uptime', 'r') as f:
        uptime_seconds = float(f.readline().split()[0])
        uptime = str(timedelta(seconds=uptime_seconds))
    print(uptime)

manage.forever(stop_after_exc=False)

Contrived example,

from gevent.monkey import patch_all
patch_all()

from gevent_tasks import Task, TaskManager, TaskPool

from myapp.tasks import check_websockets, check_uptime, check_health

pool = TaskPool(size=25)
manager = TaskManager(pool=pool)

manager.add_many(
    Task('WebsocketHealth', check_websockets, interval=7.5),
    Task('ApplicationHealth', check_uptime, interval=30.0),
    Task('SystemHealth', check_health, args=('localhost',), interval=2.5)
)
manager.start_all()
..
..
http_server.serve_forever()

Using the parse-crontab module we're able to define intervals with cron syntax,

from gevent_tasks import Task, cron
..
..
Task('ShowCharts', show_charts, interval=cron('* * * * *'), timeout=30.0)

The manager instance can also be used to register tasks via decorator. Calling TaskManager.forever() will block the code until there are no longer scheduled tasks or until an Exception is thrown inside one of the running Tasks.

manage = TaskManager()

@manage.task(interval=cron('* * * * *'))
def every_minute(task, *args):
    print('hi', args, task, task.timing)

manage.forever()

You can also reference the previous return value, allowing tasks to build on themselves over time without human / programmatic interaction.

@manage.task(interval=1)
def random_number(task):
    num = random.randint(0, 100)
    print(task.value, num)
    return num

.. output ..

None 51
51 50
50 88
88 26

Attribution

This module relies primarily on the gevent project for all its core functionality.

MIT License

Copyright (c) 2017 Blake VandeMerwe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

gevent-tasks-0.3.1.tar.gz (14.8 kB view details)

Uploaded Source

File details

Details for the file gevent-tasks-0.3.1.tar.gz.

File metadata

  • Download URL: gevent-tasks-0.3.1.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.11 CPython/3.7.2 Linux/4.20.6-arch1-1-ARCH

File hashes

Hashes for gevent-tasks-0.3.1.tar.gz
Algorithm Hash digest
SHA256 01af4bc8280f19e8da27f2ffd1948f1c813bf6988d2ed7d60061775a0c5187c1
MD5 2de844b8e640390c9c579bec08c7ce16
BLAKE2b-256 da3d0c9e0ed1dca4369be028b3f81d3346aeddfc300880aeb5afe7ca3d1bf1d0

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