Skip to content

hinohi/simple_task_queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple task queue

What

Python library.

  • woking in single host, single process
  • no middleware

I make this to use in chat-bot framework like slack with interactive mode.

install

TBA

How to use

most simple use

from simple_task_queue import task, Task, WorkerPool

@task('add')  # register task
def add(a, b):
    return a + b

if __name__ == '__main__':
    with WorkerPool():
        print(Task('add')(2, 3))  # 5

async

from simple_task_queue import task, Task, WorkerPool

@task('div')
def div(a, b):
    return a / b

if __name__ == '__main__':
    with WorkerPool():
        t = Task('div')
        t1 = t.promise(1, 0)
        t2 = t.promise(3, 2)
        # do something
        print(t2.result())  # 1.5
        print(t1.result())  # raise ZeroDivisionError

use priority

from simple_task_queue import task, Task, WorkerPool

@task('add')
def add(a, b):
    return a + b

@task('background.sleep')
def sleep(name, sec):
    import time
    print('sleep:', name)
    time.sleep(sec)
    print('wake up:', name)

if __name__ == '__main__':
    with WorkerPool(worker_num=10, default_priority=50) as pool:
        # add 'background' queue
        pool.add_queue('background', priority=30)
        for i in range(20):
            # sleepy task goto 'background' queue
            Task('background.sleep').promise(i, 3)
        # This task may be evaluated 11th
        print(Task('add')(4, 3))
    # when exit 'with' join all task

About

Python single host and threading base task queue

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages