I have long programming background, but am new to Python, and am playing around with Tornado, to prototype a certain long poll service.
What I want to achieve is that user connects to say example.com/get/1234, which is the long poll part. 1234 is the user ID. For now, it just hangs and waits for the content. Then user uses a new tab/other browser/other computer/etc and goes to url like example.com/set/1234?data=abcd, where 1234 is the user ID and data is a variable with content “abcd”. Now when this happens, the first get request should print out the data “abcd” and finish the request. User ID is used obviously to allow multiple users to use the service simultaneously. So simply put:
1) Go to example.com/get/1234 -> waiting
2) In another tab, open example.com/set/1234?data=abcd
3) Right after this request, first request prints out abcd and finishes
Below is something I’ve been trying, but I’m not really advancing with this, nor cannot find proper Google keywords to solve this.
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()
All help very much appreciated. Thanks in advance!
I actually managed to fix this, found the solution.
Just to recap in case someone else is looking into this: I saved the listeners to a dict with the
user_idand when the/setwas called, i messaged the listener with the sameuser_id. If someone is interested, I can share more code.