Skip to main content

Background worker based on pickle and sqlite

Project description

kerground

Downloads PyPI

Background worker based on pickle, sqlite and multiprocessing.

Quickstart

Install

pip install kerground

Mark your file workers by naming them with _worker.py prefix

my_worker.py

Kerground will look in *_worker.py and will consider each function an event. Functions from *_worker.py files must be unique

Import Kerground, instantiate it and start sending events

from kerground import Kerground

ker = Kerground()

@app.route('/some-task')
def long_wait():
    id = ker.send('long_task') 
    # 'long_task' is a function name from *_worker.py files
    return {'id': id}

Your api's and workers must be in the same package/directory

root
├── api
│   ├── __init__.py
│   └── my_api.py
└── worker
    ├── __init__.py
    └── my_worker.py

You are free to use any folder structure.

Open 2 cmd/terminal windows in the example directory:

  • in one start your api python3 api/my_api.py
  • in the other one type kerground

kerground_example.gif

API

ker.send('func_name', *func_args)

Send event to kerground worker. send function will return the id of the task sent to the worker. You have hot reload on your workers by default! (as long you don't change function names)

ker.status(id)

Check status of a task with status. Kerground has the folowing statuses:

  • pending - event is added to kerground queue
  • running - event is running
  • finished - event was executed succesfully
  • failed - event failed to be executed

Also you can check at any time the statuses of your tasks without specifing the id's:

ker.pending() 
ker.running()
ker.finished()
ker.failed()

Or check all statuses with:

ker.stats()

ker.get_response(id)

Get the response from event (will be None if event didn't ran yet).

You can see functions collected from *_worker.py files with:

ker.events

Why

Under the hood kerground uses pickle for serialization of input/output data, a combination of inspect methods and built-in getattr function for dynamically calling the "events"(functions) from *_worker.py files. It's resource frendly (it doesn't use RAM to hold queue), easy to use (import kerground, mark your worker files with _worker.py prefix and you are set), has hot reload for workers (no need to restart workers each time you make a change) works on multiple cores (uses multiprocessing).

Submit any questions/issues you have! Fell free to fork it and improve it!

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

kerground-0.0.4.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

kerground-0.0.4-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

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