本文整理汇总了Python中tornado.ioloop.IOLoop.close方法的典型用法代码示例。如果您正苦于以下问题:Python IOLoop.close方法的具体用法?Python IOLoop.close怎么用?Python IOLoop.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.ioloop.IOLoop
的用法示例。
在下文中一共展示了IOLoop.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SyncHTTPClientTest
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
class SyncHTTPClientTest(unittest.TestCase):
def setUp(self):
self.server_ioloop = IOLoop()
event = threading.Event()
@gen.coroutine
def init_server():
sock, self.port = bind_unused_port()
app = Application([("/", HelloWorldHandler)])
self.server = HTTPServer(app)
self.server.add_socket(sock)
event.set()
def start():
self.server_ioloop.run_sync(init_server)
self.server_ioloop.start()
self.server_thread = threading.Thread(target=start)
self.server_thread.start()
event.wait()
self.http_client = HTTPClient()
def tearDown(self):
def stop_server():
self.server.stop()
# Delay the shutdown of the IOLoop by several iterations because
# the server may still have some cleanup work left when
# the client finishes with the response (this is noticeable
# with http/2, which leaves a Future with an unexamined
# StreamClosedError on the loop).
@gen.coroutine
def slow_stop():
# The number of iterations is difficult to predict. Typically,
# one is sufficient, although sometimes it needs more.
for i in range(5):
yield
self.server_ioloop.stop()
self.server_ioloop.add_callback(slow_stop)
self.server_ioloop.add_callback(stop_server)
self.server_thread.join()
self.http_client.close()
self.server_ioloop.close(all_fds=True)
def get_url(self, path):
return "http://127.0.0.1:%d%s" % (self.port, path)
def test_sync_client(self):
response = self.http_client.fetch(self.get_url("/"))
self.assertEqual(b"Hello world!", response.body)
def test_sync_client_error(self):
# Synchronous HTTPClient raises errors directly; no need for
# response.rethrow()
with self.assertRaises(HTTPError) as assertion:
self.http_client.fetch(self.get_url("/notfound"))
self.assertEqual(assertion.exception.code, 404)
示例2: run_auth_server
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
def run_auth_server():
client_store = ClientStore()
client_store.add_client(client_id="abc", client_secret="xyz",
redirect_uris=[],
authorized_grants=[oauth2.grant.ClientCredentialsGrant.grant_type])
token_store = TokenStore()
# Generator of tokens
token_generator = oauth2.tokengenerator.Uuid4()
token_generator.expires_in[oauth2.grant.ClientCredentialsGrant.grant_type] = 3600
provider = Provider(access_token_store=token_store,
auth_code_store=token_store, client_store=client_store,
token_generator=token_generator)
# provider.add_grant(AuthorizationCodeGrant(site_adapter=TestSiteAdapter()))
provider.add_grant(ClientCredentialsGrant())
try:
app = Application([
url(provider.authorize_path, OAuth2Handler, dict(provider=provider)),
url(provider.token_path, OAuth2Handler, dict(provider=provider)),
])
app.listen(8080)
print("Starting OAuth2 server on http://localhost:8080/...")
IOLoop.current().start()
except KeyboardInterrupt:
IOLoop.close()
示例3: loop
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
def loop():
IOLoop.clear_instance()
loop = IOLoop()
loop.make_current()
yield loop
loop.stop()
loop.close()
示例4: test__yield_for_all_futures
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
def test__yield_for_all_futures():
loop = IOLoop()
loop.make_current()
@gen.coroutine
def several_steps():
value = 0
value += yield async_value(1)
value += yield async_value(2)
value += yield async_value(3)
raise gen.Return(value)
result = {}
def on_done(future):
result["value"] = future.result()
loop.stop()
loop.add_future(yield_for_all_futures(several_steps()), on_done)
try:
loop.start()
except KeyboardInterrupt as e:
print("keyboard interrupt")
assert 6 == result["value"]
loop.close()
示例5: Client
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
class Client(object):
def __init__(self, host, port, timeout=None, connect_timeout=-1, unix_socket=None, max_buffer_size=104857600):
self._io_loop = IOLoop()
self._async_client = AsyncClient(host, port, unix_socket, self._io_loop, timeout, connect_timeout, max_buffer_size)
self._response = None
self._closed = False
def __del__(self):
self.close()
@property
def closed(self):
return self._async_client.closed
def close(self):
if not self._closed:
self._async_client.close()
self._io_loop.close()
self._closed = True
def call(self, request):
def callback(response):
self._response = response
self._io_loop.stop()
self._async_client.call(request, callback)
self._io_loop.start()
response = self._response
self._response = None
response.rethrow()
return response
def __str__(self):
return str(self._async_client)
示例6: run_auth_server
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
def run_auth_server():
client_store = ClientStore()
client_store.add_client(client_id="abc", client_secret="xyz", redirect_uris=["http://localhost:8081/callback"])
token_store = TokenStore()
provider = Provider(
access_token_store=token_store, auth_code_store=token_store, client_store=client_store, token_generator=Uuid4()
)
provider.add_grant(AuthorizationCodeGrant(site_adapter=TestSiteAdapter()))
try:
app = Application(
[
url(provider.authorize_path, OAuth2Handler, dict(provider=provider)),
url(provider.token_path, OAuth2Handler, dict(provider=provider)),
]
)
app.listen(8080)
print("Starting OAuth2 server on http://localhost:8080/...")
IOLoop.current().start()
except KeyboardInterrupt:
IOLoop.close()
示例7: test_close_file_object
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
def test_close_file_object(self):
"""When a file object is used instead of a numeric file descriptor,
the object should be closed (by IOLoop.close(all_fds=True),
not just the fd.
"""
# Use a socket since they are supported by IOLoop on all platforms.
# Unfortunately, sockets don't support the .closed attribute for
# inspecting their close status, so we must use a wrapper.
class SocketWrapper(object):
def __init__(self, sockobj):
self.sockobj = sockobj
self.closed = False
def fileno(self):
return self.sockobj.fileno()
def close(self):
self.closed = True
self.sockobj.close()
sockobj, port = bind_unused_port()
socket_wrapper = SocketWrapper(sockobj)
io_loop = IOLoop()
io_loop.add_handler(socket_wrapper, lambda fd, events: None,
IOLoop.READ)
io_loop.close(all_fds=True)
self.assertTrue(socket_wrapper.closed)
示例8: HTTPClient
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
class HTTPClient(object):
""" 阻塞式的HTTP客户端。使用ioloop+异步HTTP客户端实现,基本只是用于测试。 """
def __init__(self, async_client_class=None, **kwargs):
self._io_loop = IOLoop()
if async_client_class is None:
async_client_class = AsyncHTTPClient
self._async_client = async_client_class(self._io_loop, **kwargs)
self._response = None
self._closed = False
def __del__(self):
self.close()
def close(self):
if not self._closed:
self._async_client.close()
self._io_loop.close()
self._closed = True
def fetch(self, request, **kwargs):
def callback(response):
self._response = response
self._io_loop.stop()
self._async_client.fetch(request, callback, **kwargs)
self._io_loop.start()
response = self._response
self._response = None
response.rethrow()
return response
示例9: ReactorTestCase
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
class ReactorTestCase(unittest.TestCase):
def setUp(self):
self._io_loop = IOLoop()
self._reactor = TornadoReactor(self._io_loop)
def tearDown(self):
self._io_loop.close(all_fds=True)
示例10: run_worker_fork
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
def run_worker_fork(q, ip, scheduler_ip, scheduler_port, ncores, nanny_port,
worker_port, local_dir, services, name, memory_limit):
""" Function run by the Nanny when creating the worker """
from distributed import Worker # pragma: no cover
from tornado.ioloop import IOLoop # pragma: no cover
IOLoop.clear_instance() # pragma: no cover
loop = IOLoop() # pragma: no cover
loop.make_current() # pragma: no cover
worker = Worker(scheduler_ip, scheduler_port, ncores=ncores, ip=ip,
service_ports={'nanny': nanny_port}, local_dir=local_dir,
services=services, name=name, memory_limit=memory_limit,
loop=loop) # pragma: no cover
@gen.coroutine # pragma: no cover
def start():
try: # pragma: no cover
yield worker._start(worker_port) # pragma: no cover
except Exception as e: # pragma: no cover
logger.exception(e) # pragma: no cover
q.put(e) # pragma: no cover
else:
assert worker.port # pragma: no cover
q.put({'port': worker.port, 'dir': worker.local_dir}) # pragma: no cover
loop.add_callback(start) # pragma: no cover
try:
loop.start() # pragma: no cover
finally:
loop.stop()
loop.close(all_fds=True)
示例11: SocketServerThreadStarter
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
class SocketServerThreadStarter(Thread):
'''
Used to fire up the three services each in its own thread.
'''
def __init__(self, socketServerClassName, port):
'''
Create one thread for one of the services to run in.
@param socketServerClassName: Name of top level server class to run.
@type socketServerClassName: string
@param port: port to listen on
@type port: int
'''
super(SocketServerThreadStarter, self).__init__();
self.socketServerClassName = socketServerClassName;
self.port = port;
self.ioLoop = None;
def stop(self):
self.ioLoop.stop();
def run(self):
'''
Use the service name to instantiate the proper service, passing in the
proper helper class.
'''
super(SocketServerThreadStarter, self).run();
try:
if self.socketServerClassName == 'RootWordSubmissionService':
EchoTreeService.log("Starting EchoTree new tree submissions server %d: accepts word trees submitted from connecting clients." % self.port);
http_server = RootWordSubmissionService(RootWordSubmissionService.handle_request);
http_server.listen(self.port);
self.ioLoop = IOLoop();
self.ioLoop.start();
self.ioLoop.close(all_fds=True);
return;
elif self.socketServerClassName == 'EchoTreeScriptRequestHandler':
EchoTreeService.log("Starting EchoTree script server %d: Returns one script that listens to the new-tree events in the browser." % self.port);
http_server = EchoTreeScriptRequestHandler(EchoTreeScriptRequestHandler.handle_request);
http_server.listen(self.port);
self.ioLoop = IOLoop();
self.ioLoop.start();
self.ioLoop.close(all_fds=True);
return;
else:
raise ValueError("Service class %s is unknown." % self.socketServerClassName);
except Exception:
# Typically an exception is caught here that complains about 'socket in use'
# Should avoid that by sensing busy socket and timing out:
# if e.errno == 98:
# print "Exception: %s. You need to try starting this service again. Socket busy condition will time out within 30 secs or so." % `e`
# else:
# print `e`;
#raise e;
pass
finally:
if self.ioLoop is not None and self.ioLoop.running():
self.ioLoop.stop();
return;
示例12: test_default_current
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
def test_default_current(self):
self.io_loop = IOLoop()
# The first IOLoop with default arguments is made current.
self.assertIs(self.io_loop, IOLoop.current())
# A second IOLoop can be created but is not made current.
io_loop2 = IOLoop()
self.assertIs(self.io_loop, IOLoop.current())
io_loop2.close()
示例13: cluster
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
def cluster(nworkers=2, nanny=False, worker_kwargs={}):
if nanny:
_run_worker = run_nanny
else:
_run_worker = run_worker
scheduler_q = Queue()
scheduler = Process(target=run_scheduler, args=(scheduler_q,))
scheduler.daemon = True
scheduler.start()
sport = scheduler_q.get()
workers = []
for i in range(nworkers):
q = Queue()
fn = '_test_worker-%s' % uuid.uuid1()
proc = Process(target=_run_worker, args=(q, sport),
kwargs=merge({'ncores': 1, 'local_dir': fn},
worker_kwargs))
workers.append({'proc': proc, 'queue': q, 'dir': fn})
for worker in workers:
worker['proc'].start()
for worker in workers:
worker['port'] = worker['queue'].get()
loop = IOLoop()
s = rpc(ip='127.0.0.1', port=sport)
start = time()
try:
while True:
ncores = loop.run_sync(s.ncores)
if len(ncores) == nworkers:
break
if time() - start > 5:
raise Exception("Timeout on cluster creation")
yield {'proc': scheduler, 'port': sport}, workers
finally:
logger.debug("Closing out test cluster")
with ignoring(socket.error, TimeoutError, StreamClosedError):
loop.run_sync(lambda: disconnect('127.0.0.1', sport), timeout=0.5)
scheduler.terminate()
scheduler.join(timeout=2)
for port in [w['port'] for w in workers]:
with ignoring(socket.error, TimeoutError, StreamClosedError):
loop.run_sync(lambda: disconnect('127.0.0.1', port),
timeout=0.5)
for proc in [w['proc'] for w in workers]:
with ignoring(Exception):
proc.terminate()
proc.join(timeout=2)
for q in [w['queue'] for w in workers]:
q.close()
for fn in glob('_test_worker-*'):
shutil.rmtree(fn)
loop.close(all_fds=True)
示例14: XDebugServer
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
class XDebugServer(TCPServer):
"""Class to listen for xdebug requests"""
def __init__(self):
"""Constructor """
self.ioloop = IOLoop()
super(XDebugServer, self).__init__(io_loop=self.ioloop)
self.listen(9000)
# this is for cross thread communication
self.inport = Queue()
self.outport = Queue()
self._xdebug_connection = None
def listenfunc():
self.ioloop.make_current()
self.ioloop.start()
self.ioloop.close(all_fds=True)
self.listener_thread = threading.Thread(target=listenfunc)
self.listener_thread.daemon = True
self.listener_thread.start()
def handle_stream(self, stream, address):
"""Handle a connection
Only one connection at a time, for now
:stream: @todo
:address: @todo
:returns: @todo
"""
self._xdebug_connection = XDebugConnection(self, stream, address)
def run_command(self, command, data=None):
"""Send status
:returns: @todo
"""
self.inport.put("{} -i 1\0".format(str(command)))
return self.outport.get()
def stop(self):
"""Stop tornado event loop
:returns: @todo
"""
self.ioloop.stop()
self.listener_thread.join()
del self.ioloop
del self.listener_thread
示例15: pristine_loop
# 需要导入模块: from tornado.ioloop import IOLoop [as 别名]
# 或者: from tornado.ioloop.IOLoop import close [as 别名]
def pristine_loop():
IOLoop.clear_instance()
loop = IOLoop()
loop.make_current()
try:
yield loop
finally:
loop.close(all_fds=True)
IOLoop.clear_instance()