不要等待异步功能完成 [英] Don't wait for an async function to finish

查看:101
本文介绍了不要等待异步功能完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异步龙卷风服务器,它调用异步功能.但是,该功能只是进行一些后台处理,我不想等待它完成.我怎样才能做到这一点?这是我所拥有的示例:

I have a async tornado server that calls an async function. However, that function just does some background processing, and I don't want to wait for it to finish. How can I do this? Here is an example of what I have:

@gen.coroutine
def get(self):
    yield self.process('data') # I don't want to wait here
    self.write('page')

    @gen.coroutine
    def process(self, arg):
        d = yield gen.Task(self.otherFunc, arg)
        raise gen.Return(None)

推荐答案

只需在self.process('data')之前删除收益.它仍然会运行,但是get函数不会等待它完成.示例:

Simply remove the yield before self.process('data'). It will still run, but the get function won't wait for it to finish. Example:

@gen.coroutine
def get(self):
    print 'a'
    yield self.process('data') # I don't want to wait here
    print 'b'
    self.write('page')

@gen.coroutine
def process(self, arg):
    print 'c'
    d = yield gen.Task(self.otherFunc, arg)
    print 'd'
    raise gen.Return(None)

将给出a,c,d,b,但:

Will give a,c,d,b but:

@gen.coroutine
def get(self):
    print 'a'
    self.process('data') # I don't want to wait here
    print 'b'
    self.write('page')

@gen.coroutine
def process(self, arg):
    print 'c'
    d = yield gen.Task(self.otherFunc, arg)
    print 'd'
    raise gen.Return(None)

可以根据订单执行情况给出a,c,b,d或a,b,c,d,但是它将不再等到完成处理后才能到达"b".

Can give a,c,b,d or a,b,c,d depending on order execution, but it will no longer wait until process is done to get to 'b'.

这篇关于不要等待异步功能完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆