私はトルネードからコルーチンを理解する過程にあるので、すべてをシンプルに保ち、ペーストするコードが多くなるほどよいでしょう。トルネードコルーチン - カスタム関数
私が欲しいのは、自分の手作りの機能を非同期にすることです。
ドキュメントにあるすべての例は、同じ「隠された」部分、つまりAsyncHTTPClientに該当します。私はHTTPコールをするつもりはない。だから、そのクラスで私に例を挙げてはいけません。私は最初から何かを作りたいと思っています。私はすべての可能性を試しましたTornado coroutine
今のところ私はbashの睡眠でテストしています。
import tornado.web
import tornado.httpserver
import tornado.gen
import tornado.concurrent
import subprocess
import os
@tornado.gen.coroutine
def letswait():
fut = tornado.concurrent.Future()
subprocess.check_output(["sleep", "5"])
fut.set_result(42)
return fut
class TestHandler1(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
value = yield letswait()
self.render("test.html", num=value)
class TestHandler2(tornado.web.RequestHandler):
def get(self):
self.render("test.html", num=66)
class Application(tornado.web.Application):
def __init__(self):
DIRNAME = os.path.dirname(__file__)
STATIC_PATH = os.path.join(DIRNAME, '../static')
TEMPLATE_PATH = os.path.join(DIRNAME, '../template')
sets = {
"template_path":TEMPLATE_PATH,
"static_path":STATIC_PATH,
"debug":True,
}
tornado.web.Application.__init__(self, [
(r"/test1", TestHandler1),
(r"/test2", TestHandler2),
], **sets)
def main():
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(8888)
print "Let s start"
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
私がtest1にアクセスすると、test2にアクセスする前に呼び出しが返るのを待つ必要があります。私が理解したところから、gen.sleep(5)
を使用する必要があります。しかしそれは単なる例です。たとえば、sleep 5
をbashで実行する代わりに、実行するのに時間がかかるssh somewhere 'do_something'
を実行しています。
「この機能は非同期ではありません」と言われました。だから私の質問は、カスタム関数を非同期にする方法です。
EDIT:少し検索したところで、私はここに竜巻のプロセスhttps://gist.github.com/FZambia/5756470があるのを見ました。しかし、私のサブプロセスはサードパーティ製のものなので、上書きできるものではありません。だから私の質問も、私はそのgen.coroutineシステムとサードパーティのライブラリを統合するのですか?
SOLUTION:私はここに同様の質問求めてきました
import tornado.web
import tornado.httpserver
import tornado.gen
import tornado.concurrent
import subprocess
import os
from concurrent import futures
# Create a threadpool, and this can be shared around different python files
# which will not re-create 10 threadpools when we call it.
# we can a handful of executors for running synchronous tasks
# Create a 10 thread threadpool that we can use to call any synchronous/blocking functions
executor = futures.ThreadPoolExecutor(10)
def letswait():
result_future = tornado.concurrent.Future()
subprocess.check_output(["sleep", "5"])
result_future.set_result(42)
return result_future
class TestHandler1(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
value = yield executor.submit(letswait)
self.render("test.html", num=value)
class TestHandler2(tornado.web.RequestHandler):
def get(self):
self.render("test.html", num=66)
class Application(tornado.web.Application):
def __init__(self):
DIRNAME = os.path.dirname(__file__)
STATIC_PATH = os.path.join(DIRNAME, '../static')
TEMPLATE_PATH = os.path.join(DIRNAME, '../template')
sets = {
"template_path":TEMPLATE_PATH,
"static_path":STATIC_PATH,
"debug":True,
}
tornado.web.Application.__init__(self, [
(r"/test1", TestHandler1),
(r"/test2", TestHandler2),
], **sets)
def main():
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(8888)
print "Let s start"
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
.result()を削除できますか?それは私の間違いでした。歩留まりは自動的に結果を得るはずです。あなたが結果を待っている可能性があります()と、それはブロックになります。 – user1157751