« Back to Index

[Python Tornado Queue]

View original Gist on GitHub

Tags: #python #tornado #queue

Python Tornado Queue.py

import logging
import time

import tornado.gen
import tornado.ioloop
import tornado.queues
import tornado.web


class Client():
    def __init__(self):
        self.queue = tornado.queues.Queue()

    async def watch_queue(self):
        while True:
            task = await self.queue.get()
            self.run_task(task)

    def run_task(self, task):
        self.queue.task_done()


client = Client()


class AppHandler(tornado.web.RequestHandler):
    async def get(self):
        await client.queue.put("%f" % time.time())
        logging.warn(f'queue ({client.queue})')
        self.write("Queued a new item")


if __name__ == "__main__":
    tornado.ioloop.IOLoop.instance().add_callback(client.watch_queue)

    application = tornado.web.Application([
        (r'/', AppHandler),
    ], debug=True)

    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()