擅长:python、mysql、java
<p>现在<a href="http://www.tornadoweb.org/en/stable/guide/async.html#examples" rel="nofollow noreferrer">Documentation</a>有解了。</p>
<p>简单示例:</p>
<pre><code>import os.path
import tornado.web
from tornado import gen
class MyHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self, filename):
result = yield self.some_usefull_process(filename)
self.write(result)
@gen.coroutine
def some_usefull_process(self, filename):
if not os.path.exists(filename):
status = yield self.generate_file(filename)
result = 'File created'
else:
result = 'File exists'
raise gen.Return(result)
@gen.coroutine
def generate_file(self, filename):
fd = open(filename, 'w')
fd.write('created')
fd.close()
</code></pre>