python - Tornado asynchronous job in tornado gen coroutine -


i've written application takes job queue , executes asynchronously.

def job(self):     print 'in job'     time.sleep(0.01)  @gen.coroutine def start_jobs(self):     jobs = filter(lambda x: x['status'] == 0, self.queue)     job in jobs:         yield self.job()     print 'exit start job' 

but, code not work.

output:

in job

in job

in job etc

how do correctly?

how make work futures, , and there simpler way tornado?

never call time.sleep in tornado! use yield gen.sleep instead.

install toro pip install toro , use joinablequeue:

import random tornado import ioloop, gen import toro   class c(object):     def __init__(self):         self.queue = toro.joinablequeue()      @gen.coroutine     def start_jobs(self):         while true:             job_id = yield self.queue.get()             self.job(job_id)      @gen.coroutine     def job(self, job_id):         print 'job_id', job_id         yield gen.sleep(random.random())         print 'job_id', job_id, 'done'         self.queue.task_done()   c = c() in range(5):     c.queue.put_nowait(i)  c.start_jobs()  io_loop = ioloop.ioloop.instance()  # block until tasks done c.queue.join().add_done_callback(lambda future: io_loop.stop()) io_loop.start() 

starting tornado 4.2, toro part of tornado, can queue = tornado.queues.queue() instead of using toro joinablequeue:

http://tornado.readthedocs.org/en/latest/releases/v4.2.0.html#new-modules-tornado-locks-and-tornado-queues


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -