Python Tornado gen协同程序中的Tornado异步作业

Python Tornado gen协同程序中的Tornado异步作业,python,asynchronous,tornado,Python,Asynchronous,Tornado,我编写了一个应用程序,它从队列中获取作业并异步执行 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) for job in jobs: yield self.job() print 'exit from start

我编写了一个应用程序,它从队列中获取作业并异步执行

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)
    for job in jobs:
        yield self.job()
    print 'exit from start job'
但是,此代码不起作用

输出:

在职

在职

在职等

我如何正确地做它


我如何让它与期货一起工作,有没有更简单的方法与Tornado一起工作?

永远不要调用
时间。在Tornado中睡觉!改用
yield gen.sleep

使用
pip安装Toro
安装Toro并使用可连接队列:

import random
from 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()
for i in range(5):
    c.queue.put_nowait(i)

c.start_jobs()

io_loop = ioloop.IOLoop.instance()

# block until all tasks are done
c.queue.join().add_done_callback(lambda future: io_loop.stop())
io_loop.start()
从Tornado 4.2开始,Toro是Tornado的一部分,因此您只需执行
queue=Tornado.queues.queue()
,而不用使用Toro JoinableQueue:

import random
from 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()
for i in range(5):
    c.queue.put_nowait(i)

c.start_jobs()

io_loop = ioloop.IOLoop.instance()

# block until all tasks are done
c.queue.join().add_done_callback(lambda future: io_loop.stop())
io_loop.start()

千万不要叫时间。在龙卷风中睡觉吧!改用
yield gen.sleep

使用
pip安装Toro
安装Toro并使用可连接队列:

import random
from 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()
for i in range(5):
    c.queue.put_nowait(i)

c.start_jobs()

io_loop = ioloop.IOLoop.instance()

# block until all tasks are done
c.queue.join().add_done_callback(lambda future: io_loop.stop())
io_loop.start()
从Tornado 4.2开始,Toro是Tornado的一部分,因此您只需执行
queue=Tornado.queues.queue()
,而不用使用Toro JoinableQueue:

import random
from 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()
for i in range(5):
    c.queue.put_nowait(i)

c.start_jobs()

io_loop = ioloop.IOLoop.instance()

# block until all tasks are done
c.queue.join().add_done_callback(lambda future: io_loop.stop())
io_loop.start()

千万不要叫时间。在龙卷风中睡觉吧!改用
yield gen.sleep

使用
pip安装Toro
安装Toro并使用可连接队列:

import random
from 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()
for i in range(5):
    c.queue.put_nowait(i)

c.start_jobs()

io_loop = ioloop.IOLoop.instance()

# block until all tasks are done
c.queue.join().add_done_callback(lambda future: io_loop.stop())
io_loop.start()
从Tornado 4.2开始,Toro是Tornado的一部分,因此您只需执行
queue=Tornado.queues.queue()
,而不用使用Toro JoinableQueue:

import random
from 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()
for i in range(5):
    c.queue.put_nowait(i)

c.start_jobs()

io_loop = ioloop.IOLoop.instance()

# block until all tasks are done
c.queue.join().add_done_callback(lambda future: io_loop.stop())
io_loop.start()

千万不要叫时间。在龙卷风中睡觉吧!改用
yield gen.sleep

使用
pip安装Toro
安装Toro并使用可连接队列:

import random
from 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()
for i in range(5):
    c.queue.put_nowait(i)

c.start_jobs()

io_loop = ioloop.IOLoop.instance()

# block until all tasks are done
c.queue.join().add_done_callback(lambda future: io_loop.stop())
io_loop.start()
从Tornado 4.2开始,Toro是Tornado的一部分,因此您只需执行
queue=Tornado.queues.queue()
,而不用使用Toro JoinableQueue:

import random
from 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()
for i in range(5):
    c.queue.put_nowait(i)

c.start_jobs()

io_loop = ioloop.IOLoop.instance()

# block until all tasks are done
c.queue.join().add_done_callback(lambda future: io_loop.stop())
io_loop.start()

如果我想添加一些自定义函数,而不是gen.sleep(random.random()),该怎么办?我想添加使用job_id的函数并进行长时间的计算。当然,只需使用“yield”调用该函数即可。请参阅我的文章emptysqua.re/blog/refactoring-tornado-coroutines/如果我想添加一些自定义函数而不是gen.sleep(random.random()),该怎么办?我想添加使用job_id的函数并进行长时间的计算。当然,只需使用“yield”调用该函数即可。请参阅我的文章emptysqua.re/blog/refactoring-tornado-coroutines/如果我想添加一些自定义函数而不是gen.sleep(random.random()),该怎么办?我想添加使用job_id的函数并进行长时间的计算。当然,只需使用“yield”调用该函数即可。请参阅我的文章emptysqua.re/blog/refactoring-tornado-coroutines/如果我想添加一些自定义函数而不是gen.sleep(random.random()),该怎么办?我想添加使用job_id的函数并进行长时间的计算。当然,只需使用“yield”调用该函数即可。请参阅我的文章emptysqua.re/blog/refactoring-tornado-coroutines/