Don't wait for an async function to finish

强颜欢笑 提交于 2020-01-05 09:11:37

问题


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)

回答1:


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)

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)

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'.



来源:https://stackoverflow.com/questions/25083084/dont-wait-for-an-async-function-to-finish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!