没有调用Tornado io_loop add_future回调

时间:2015-11-18 18:08:28

标签: python callback websocket tornado

我正在研究Tornado WebSocket服务器,它在收到消息时执行一些异步操作。 我在io_loop.add_future中添加了一个回调函数,以便在函数结束时或者引发异常时收到通知,但是从不调用此回调函数。

这是正在发生的事情的一个例子:

import tornado
from tornado.httpclient import HTTPRequest
from tornado.web import Application
from tornado.websocket import websocket_connect
from tornado.testing import AsyncHTTPTestCase, gen_test

def message_processed_callback(*args, **kwargs):
    print 'Callback(args=%r, kwargs=%r)' % (args, kwargs)

class RealtimeHandler(tornado.websocket.WebSocketHandler):
    def initialize(self):
        self.io_loop = tornado.ioloop.IOLoop.instance()

    def on_message(self, message):
        future = self.on_some_message(message)
        print 'The future:', future
        self.io_loop.add_future(future, message_processed_callback)

    @tornado.gen.coroutine
    def on_some_message(self, message):
        print 'Before sleep'
        yield tornado.gen.sleep(3)
        print 'After sleep'
        self.write_message(message)

class ChatTestCase(AsyncHTTPTestCase):
    def get_app(self):
        return Application([
            ('/rt', RealtimeHandler),
        ])

    @gen_test
    def test_reply(self):
        request = HTTPRequest('ws://127.0.0.1:%d/rt' % self.get_http_port())
        ws = yield websocket_connect(request)

        ws.write_message('Hi')

        response = yield ws.read_message()
        print 'Response:', response

执行此测试会导致:

Before sleep
The future: <tornado.concurrent.Future object at 0x4c01250>
[The sleep takes place here]
After sleep
Response: Hi

1 个答案:

答案 0 :(得分:0)

为了确保测试之间的隔离,每个测试用例都运行独立的IOLoop - 而不是IOLoop.instance()。可以使用IOLoop访问测试IOLoop.current()(应该几乎总是使用IOLoop.instance()而不是IOLoop.instance())。

如果由于您无法控制的库而要求使用IOLoop,则可以通过覆盖AsyncTestCase.get_new_ioloop使<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/app_bar" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="?attr/actionBarSize" android:fitsSystemWindows="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Put your content View here!" /> </RelativeLayout> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout> 使用该测试。