Code Monkey home page Code Monkey logo

kewkew's Introduction

kewkew

PyPI version License

A tiny queue library leveraging asyncio for background tasks or scripting

Install

You can install kewkew via pip.

python -m pip install kewkew

You can also download the kewkew.py source from this repo. There are no external dependencies to worry about.

Usage

The Kew class is meant to be a parent class. At a minimum, the child class needs to implement an async worker method which must do two things:

  • Take a single argument that accepts data from the queue
  • Returns a boolean if the data was processed successfully and can be removed from the queue

Here is a minimum viable implementation.

from kewkew import Kew

class MyKew(Kew):
    async def worker(self, data: object) -> bool:
        print(data)
        return True

We then have two main ways of using the queue. Kew provides both sync and async methods to add items to the queue and tell the queue to finish processing (for a graceful shutdown).

This first example uses the sync calls so everything can be run in an interactive environment.

from mykew import MyKew

kew = MyKew()
for i in range(100):
    kew.add_sync(i)
kew.finish_sync()

This second example uses the async calls within a coroutine.

import asyncio as aio

async def main():
    kew = MyKew()
    for i in range(100):
        await kew.add(i)
    await kew.finish()

aio.run(main)

You can see this and an async database processing example in the examples folder.

Debug

Python Queues have a tendency to swallow runtime exceptions. You can debug your worker more easily by calling it directly.

import asyncio as aio
from mykew import MyKew

kew = MyKew()
aio.run(kew.worker(1))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.