def on_close(self):
'''
Called when client closes this connection. Cleanup
is done here.
'''
if self.id in self.funcserver.websocks:
self.funcserver.websocks[self.id] = None
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(lambda: self.funcserver.websocks.pop(self.id, None))
psession = self.funcserver.pysessions.get(self.pysession_id, None)
if psession:
psession['socks'].remove(self.id)
if not psession['socks']:
del self.funcserver.pysessions[self.pysession_id]
python类add_callback()的实例源码
def main(j, args, params, tags, tasklet):
params.merge(args)
import tornado.ioloop
def stop_portal():
ioloop = tornado.ioloop.IOLoop.current()
ioloop.add_callback(lambda x: x.sys.exit(3), tornado.ioloop)
stop_portal()
params.result = ('Portal is reloaded', params.doc)
return params
def stop_ioloop():
logging.info("stopping ws_rpc ioloop")
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(ioloop.stop)
def unload():
''' stops the http server '''
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(lambda x: x.stop(), ioloop)
_LOGGER.info('Unloaded HTTP element')
def stop(self):
"""Stop running the server"""
logging.debug("Shutting down extension server")
self.must_exit = True
if self.thread is not None:
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(ioloop.stop)
self.thread.join()
self.thread = None
logging.debug("Extension server stopped")
def call(fn):
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(fn)
def register(handlers=None, subdomain=None, parent_dir=None, urls=None):
'''
Register additional handlers to the server.
:param handlers: List of handler classes to register.
:param subdomain: The desired subdomain
:param parent_dir: The directory under which all the handlers should be
placed
:param urls: List of lists like [['path', Handler]].
Overwrites handlers input.
'''
# parse input
if subdomain is None:
subdomain = "[A-Za-z0-9.]*"
else:
subdomain = r'{}\.[A-Za-z0-9.]*'.format(subdomain)
if parent_dir is None:
parent_dir = ''
else:
parent_dir = '/{}'.format(parent_dir)
# create proper URLSpec handlers and callback
if not urls:
def doc_url(handler):
""" reads the url regexp from the handler's docstring. """
docs = handler.__doc__
pieces = docs.strip().split('\n\n')[0].split('\n')
return ''.join([piece.strip() for piece in pieces])
handlers = [tornado.web.URLSpec('{}{}'.format(parent_dir,
doc_url(handler)),
handler)
for handler in handlers]
else:
handlers = urls
def add_handler_callback(server, subdmn, hndls):
""" add handler to server """
server.add_handlers(subdmn, hndls)
# schedule handler addition to next IOLoop iteration
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(add_handler_callback, SERVER, subdomain, handlers)