LoginSignup
6
7

More than 5 years have passed since last update.

Tornado で ブロッキング処理をノンブロッキング処理化する

Last updated at Posted at 2018-04-16

Overview

Tornadoを使って
非同期処理を作るときには、対象のサービスを非同期化する必要がある。

その方法はいくつか方法があるが、最も最初に記載されているのが
@tornado.gen.coroutine を使う方法だ。

これにを使うことで、重たい処理をしていても次の処理を受け付けられるようになるということだが
例として記載されている方法は、tornadoに組み込まれている非同期処理ライブラリを使っている。
従って、外部コマンドを呼び出すなど、完全独自でブロッキング処理が入っている場合はどうすれば良いのか良く分からない。

そのための解が分かったので記載する。

具体的には3.を使うことになるが、丁寧にもloopを書いてしまっているので混乱しがちだった。
従って、単純にこうすれば良い、という例を書いておく。

単純な例

基本形は以下の通り。

import tornado.concorrent
import time

@gen.coroutine
def run(time):
    yield executor.submit(time.sleep, time)
    return 

time.sleep を呼び出す関数に置き換える。
つまりこういう感じ。

import tornado.concorrent
import time

def long_time_func(time)
    time.sleep(time) # 重たい処理
    result = time    # とりあえずreturn value を定義
    return result

@gen.coroutine
def run(time):
    yield executor.submit(long_time_func, time)
    return 

とまあ、こんな感じ。
後は、 yield で run()を呼び出してやれば良い。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7