わかりましたので、ここで私はGETリクエストでそれを行うだろうかの私の例です。
Iは、2つの主要なコンポーネントを追加しました:
最初はローカルリストオブジェクトに新しいメッセージを追加単純なねじのpubsubリスナーです。 また、クラスにリストアクセサを追加したので、あなたは普通のリストから読み込んでいるかのようにリスナースレッドから読み込むことができます。 WebRequest
に関する限り、ローカルリストオブジェクトからデータを読み込むだけです。これは即時に戻り、完了または将来の要求が受け入れられて処理されることからの現在の要求をブロックしません。
2番目はApplicationMixinクラスです。これは、機能と属性を追加するために継承するWeb要求クラスを持つ2次オブジェクトです。この場合、要求されたチャネルのチャネルリスナーがすでに存在するかどうかをチェックし、存在しない場合はチャネルリスナを作成し、WebRequestにリスナハンドルを返します。それは、静的リスト(あなたがself.write
に文字列を与える必要が念頭に)最後に
class ReadChannel(tornado.web.RequestHandler, ApplicationMixin):
@tornado.web.asynchronous
def get(self, channel):
# get the channel
channel = self.GetChannel(channel)
# write out its entire contents as a list
self.write('{}'.format(channel[:]))
self.finish() # not necessary?
であるかのようにアプリケーションが作成された後
# add a method to the application that will return existing channels
# or create non-existing ones and then return them
class ApplicationMixin(object):
def GetChannel(self, channel, host = None, port = None):
if channel not in self.application.channels:
self.application.channels[channel] = OpenChannel(channel, host, port)
self.application.channels[channel].start()
return self.application.channels[channel]
WebRequestクラスは今、リスナーを扱い、私が追加しました属性
# add a dictionary containing channels to your application
application.channels = {}
としてだけでなく、実行中のスレッドのいくつかのクリーンアップなどの空の辞書は、あなたのアプリケーションを一度終了し
# clean up the subscribed channels
for channel in application.channels:
application.channels[channel].stop()
application.channels[channel].join()
完全なコード:パイソン> = 3の場合
import threading
import redis
import tornado.web
class OpenChannel(threading.Thread):
def __init__(self, channel, host = None, port = None):
threading.Thread.__init__(self)
self.lock = threading.Lock()
self.redis = redis.StrictRedis(host = host or 'localhost', port = port or 6379)
self.pubsub = self.redis.pubsub()
self.pubsub.subscribe(channel)
self.output = []
# lets implement basic getter methods on self.output, so you can access it like a regular list
def __getitem__(self, item):
with self.lock:
return self.output[item]
def __getslice__(self, start, stop = None, step = None):
with self.lock:
return self.output[start:stop:step]
def __str__(self):
with self.lock:
return self.output.__str__()
# thread loop
def run(self):
for message in self.pubsub.listen():
with self.lock:
self.output.append(message['data'])
def stop(self):
self._Thread__stop()
# add a method to the application that will return existing channels
# or create non-existing ones and then return them
class ApplicationMixin(object):
def GetChannel(self, channel, host = None, port = None):
if channel not in self.application.channels:
self.application.channels[channel] = OpenChannel(channel, host, port)
self.application.channels[channel].start()
return self.application.channels[channel]
class ReadChannel(tornado.web.RequestHandler, ApplicationMixin):
@tornado.web.asynchronous
def get(self, channel):
# get the channel
channel = self.GetChannel(channel)
# write out its entire contents as a list
self.write('{}'.format(channel[:]))
self.finish() # not necessary?
class GetHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello world")
application = tornado.web.Application([
(r"/", GetHandler),
(r"/channel/(?P<channel>\S+)", ReadChannel),
])
# add a dictionary containing channels to your application
application.channels = {}
if __name__ == '__main__':
application.listen(8888)
print 'running'
try:
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
pass
# clean up the subscribed channels
for channel in application.channels:
application.channels[channel].stop()
application.channels[channel].join()
.listen() ' websocketの動作例については、http://tornadogists.org/532067/をご覧ください。 –
websocketは良い選択ですが、私のアプリケーションはWebSocketをサポートしていないブラウザでも動作する必要があります。私は長いポーリングを使用しています。それが私に「非同期取得」が必要な理由です。 –
@HelieelsonSantosその場合、最良の方法は、購読されたチャンネル履歴のローカル状態を(別のスレッドによって供給される)維持し、その状態をただちにレスポンスに書き込み、 'get'操作を完了することです。クライアントは、最後に取得したインデックスまたは最後に取得した時間などのレコードを維持して、異なるクライアントの連続性を維持する必要があります。私は時間を取って数時間で答えを書いてみましょう。 –