In python, Tornado is a networkframework# based on the event loop ##. It uses coroutines to handle concurrency, gen.Return("hello") is a method for returning values in coroutines. When using Tornado's asynchronous capabilities, use gen.Return() to return a value in a coroutine.
How to solveIn Tornado, use yield and gen.Return() to return values. If you want to return a value in a coroutine, you can use yield and gen.Return() to achieve your goal. For example:@gen.coroutine def my_coroutine(): result = yield some_async_call() raise gen.Return(result)
io.run() or tornado.gen.convert_yielded() outside the function to get the return value.
result = await my_coroutine()
import tornado.ioloop import tornado.gen @tornado.gen.coroutine def my_coroutine(): result = yield some_async_call() raise tornado.gen.Return(result) def handle_result(result): print(result) if __name__ == "__main__": result = tornado.ioloop.IOLoop.current().run_sync(my_coroutine) handle_result(result)
result = await my_coroutine()
async def my_coroutine(): result = await some_async_call() return result
The above is the detailed content of The solution to gen.Return(\'hello\') appearing in tornado. For more information, please follow other related articles on the PHP Chinese website!