Python Don';不要等待异步函数完成
我有一个异步tornado服务器,它调用一个异步函数。但是,该函数只执行一些后台处理,我不想等待它完成。我该怎么做?以下是我的一个例子:Python Don';不要等待异步函数完成,python,asynchronous,tornado,Python,Asynchronous,Tornado,我有一个异步tornado服务器,它调用一个异步函数。但是,该函数只执行一些后台处理,我不想等待它完成。我该怎么做?以下是我的一个例子: @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
@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函数不会等待它完成。例如:
@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,但:
@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”。请注意,如果“流程”引发异常,则不会在任何地方记录它;在未来,你还没有屈服的情况将不会被报道。考虑在您调用Purror()的站点周围使用ExtExtStAcKEnter,以便记录它引发的任何错误:在TrnADO 4中,您应该使用IOOOP.SPAWNGUI回调来启动与调用方上下文分离的回调。IOLoop将记录将来包含的任何错误。