我有很长的编程背景,但对Python还不熟悉,我正在研究Tornado,以构建一个长轮询服务的原型。在
我想实现的是用户连接说example.com/get/1234,这是长轮询部分。1234是用户ID。目前,它只是挂起并等待内容。然后用户使用一个新的tab/other browser/other computer/etc进入urlexample.com/set/1234?data=abcd,其中1234是用户ID,data是内容为“abcd”的变量。现在,当发生这种情况时,第一个get请求应该打印出数据“abcd”并完成请求。用户ID显然是用来允许多个用户同时使用服务的。所以简单地说:
1)转到example.com/get/1234->;等待
2) 在另一个选项卡中,打开example.com/set/1234?数据=abcd
3) 在这个请求之后,第一个请求打印出abcd并完成
下面是我一直在尝试的东西,但我并没有真正推进这个,也没有找到合适的谷歌关键字来解决这个问题。在class GetHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
# Getting user ID is working
def get(self, user_id):
# This is something I'm not sure is right, but found it in an
# example. I haven't created the code_received function yet,
# nor know if it should be here? Should the code_received
# and connection finishing be called from the /set part?
cursor = self.get_argument("cursor", None)
self.wait_for_code(self.code_received, cursor=cursor)
def code_received(self, data)
self.write(data)
self.finish()
非常感谢大家的帮助。提前谢谢!在