Skip to main content

A subprocess-based task queue.

Project description

soon-kyoo

A subprocess-based task queue.

Introduction

Soon-Kyoo implements a simple FIFO queue using SQLite. It was created primarily for running long simulations.

As of yet, the vision of a subprocess-based workflow has not been realized. However, the package still works as a task queue.

Installation

pip install soon-kyoo

Usage

Users must create their own subclass of soon_kyoo.BaseTask. Subclasses must define a run() method, which contains the business logic for the task (what we care about). At least for now, input arguments to this method are restricted to being JSON serializable.

Example

The following can be found in the examples directory.

timer_task.py:

"""Script for creating timer tasks.
"""

import time

import soon_kyoo as sk


def timer_sleep_all(interval, n):
    for i in range(n):
        timer_sleep(interval, i, n)


def timer_sleep(interval, i=None, n=None):
    if None not in (i, n):
        msg = f"{i+1}/{n} "
    else:
        msg = ''
    msg += f"Sleeping {interval} seconds..."
    sk.echo(msg)
    time.sleep(interval)


class TimerTask(sk.BaseTask):
    """Task to count to a certain number a specified number of times.
    """

    task_name = 'TimerTask'

    def run(self, interval, n):
        """Count at the given interval the given number of times.

        Positional arguments:
        interval - (int) Interval, in seconds.
        n - (int) Number of times to count the given interval.
        """
        self.interval = interval
        timer_sleep_all(interval, n)
        sk.echo(f'Slept {interval * n} seconds total.')


if __name__ == '__main__':
    # Create a new task.
    timer_task = TimerTask()
    # Add the task to the queue, to be executed at some future time.
    # Arguments to delay() are the same as for run().
    timer_task.delay(3, 3)

timer_worker.py

"""Script for runnning a Worker dedicated to accomplishing TimerTasks.
"""

import soon_kyoo as sk

from timer_task import TimerTask


if __name__ == "__main__":
    # Instantiate TimerTask.
    timer_task = TimerTask()

    # Run worker.
    worker = sk.Worker(task=timer_task)
    worker.start()

Running the examples

Example files are included in the examples directory. From within your repository, clone soon-kyoo (will clone into "soon-kyoo" by default)...

git clone https://github.com/n8jhj/soon-kyoo.git

...and then install it in editable mode.

pip install -e soon-kyoo

Now run the following in two separate terminals:

Terminal 1:

Run the same script a few times.

C:\Users\...>python soon-kyoo\examples\timer_task.py
Queued task: 913d56e9-a609-4b84-b937-479a94716527

C:\Users\...>python soon-kyoo\examples\timer_task.py
Queued task: da952424-98d9-42e1-8851-91a30924b94b

C:\Users\...>python soon-kyoo\examples\timer_task.py
Queued task: 7ec2887a-42a5-4cb6-a0f9-a30453d4c95c

C:\Users\...>

Terminal 2:

C:\Users\...>python soon-kyoo\examples\timer_worker.py
Running task: 913d56e9-a609-4b84-b937-479a94716527
1/3 Sleeping 3 seconds...
2/3 Sleeping 3 seconds...
3/3 Sleeping 3 seconds...
Slept 9 seconds total.
Finished task: 913d56e9-a609-4b84-b937-479a94716527

Running task: da952424-98d9-42e1-8851-91a30924b94b
1/3 Sleeping 3 seconds...
2/3 Sleeping 3 seconds...
3/3 Sleeping 3 seconds...
Slept 9 seconds total.
Finished task: da952424-98d9-42e1-8851-91a30924b94b

Running task: 7ec2887a-42a5-4cb6-a0f9-a30453d4c95c
1/3 Sleeping 3 seconds...
2/3 Sleeping 3 seconds...
3/3 Sleeping 3 seconds...
Slept 9 seconds total.
Finished task: 7ec2887a-42a5-4cb6-a0f9-a30453d4c95c

Waiting for next task... (Ctrl + C to quit)

Quitting

C:\Users\...>

Etymology

This project is named after my friend, Soon-Kyoo. People call him Q, for short.

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

soon-kyoo-0.0.4.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

soon_kyoo-0.0.4-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file soon-kyoo-0.0.4.tar.gz.

File metadata

  • Download URL: soon-kyoo-0.0.4.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.4

File hashes

Hashes for soon-kyoo-0.0.4.tar.gz
Algorithm Hash digest
SHA256 7fd5881779dce73b9974310b50121d5b82545828402a15e58fee60d470efdece
MD5 b93674ba2763ab2c473b4c3057a0f931
BLAKE2b-256 38ee019c7d21ee3a79bec8a036d3911b4d23480e2f29631ebff40e6ac054c668

See more details on using hashes here.

File details

Details for the file soon_kyoo-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: soon_kyoo-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.7 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/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.4

File hashes

Hashes for soon_kyoo-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9fec67e9cc028d10364de2da43f439f6db8db997172cf9b4abdccdb24e24b9b2
MD5 cb51228497542b684989e819cc3e7d05
BLAKE2b-256 f1b817d030685717937d1d27dbcf982533cd338028248714ca5440d4e1516763

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