dullboy
dullboy

Reputation: 225

How does @gen.coroutine work in tornado?

Could any guru here walk me through using @gen.coroutine in python 2? I read some related doc online but still couldn't understand why we need this decorator. Many thanks.

Upvotes: 1

Views: 1238

Answers (1)

bmoran
bmoran

Reputation: 1529

Take a look at the How it Works section here http://www.tornadoweb.org/en/stable/guide/coroutines.html

I would also suggest reading about decorators and generators. To sum it up, when you add the @coroutine decorator to your method, the decorator wraps your function and allows async capabilities without the need for a separate thread.

Any time you want to perform non-blocking, heavy I/O, you want to add the coroutine decorator to the method performing the operation, and yield Return() (which is actually an Exception) but allows the non-blocking to occur.

I hope this answers your questions.

Upvotes: 2

Related Questions