Skip to content Skip to sidebar Skip to footer

Don't Wait For An Async Function To Finish

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 thi

Solution 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.coroutinedefget(self):
    print'a'yield self.process('data') # I don't want to wait hereprint'b'
    self.write('page')

@gen.coroutinedefprocess(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.coroutinedefget(self):
    print'a'
    self.process('data') # I don't want to wait hereprint'b'
    self.write('page')

@gen.coroutinedefprocess(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'.

Post a Comment for "Don't Wait For An Async Function To Finish"