def __init__(self, *args, **kwargs):
"""Create a new connection to a single MongoDB instance at *host:port*.
MotorClient takes the same constructor arguments as
:class:`~pymongo.mongo_client.MongoClient`, as well as:
:Parameters:
- `io_loop` (optional): Special :class:`tornado.ioloop.IOLoop`
instance to use instead of default
"""
if 'io_loop' in kwargs:
io_loop = kwargs.pop('io_loop')
else:
io_loop = ioloop.IOLoop.current()
event_class = functools.partial(util.MotorGreenletEvent, io_loop)
kwargs['_event_class'] = event_class
super(MotorClient, self).__init__(io_loop, *args, **kwargs)
python类IOLoop()的实例源码
def __init__(self, *args, **kwargs):
"""Create a new connection to a MongoDB replica set.
MotorReplicaSetClient takes the same constructor arguments as
:class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient`,
as well as:
:Parameters:
- `io_loop` (optional): Special :class:`tornado.ioloop.IOLoop`
instance to use instead of default
"""
if 'io_loop' in kwargs:
io_loop = kwargs.pop('io_loop')
else:
io_loop = ioloop.IOLoop.current()
kwargs['_monitor_class'] = functools.partial(
MotorReplicaSetMonitor, io_loop)
super(MotorReplicaSetClient, self).__init__(io_loop, *args, **kwargs)
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def __init__(self, *args, **kwargs):
"""Create a new connection to a single MongoDB instance at *host:port*.
MotorClient takes the same constructor arguments as
:class:`~pymongo.mongo_client.MongoClient`, as well as:
:Parameters:
- `io_loop` (optional): Special :class:`tornado.ioloop.IOLoop`
instance to use instead of default
"""
if 'io_loop' in kwargs:
io_loop = kwargs.pop('io_loop')
else:
io_loop = ioloop.IOLoop.current()
event_class = functools.partial(util.MotorGreenletEvent, io_loop)
kwargs['_event_class'] = event_class
super(MotorClient, self).__init__(io_loop, *args, **kwargs)
def __init__(self, *args, **kwargs):
"""Create a new connection to a MongoDB replica set.
MotorReplicaSetClient takes the same constructor arguments as
:class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient`,
as well as:
:Parameters:
- `io_loop` (optional): Special :class:`tornado.ioloop.IOLoop`
instance to use instead of default
"""
if 'io_loop' in kwargs:
io_loop = kwargs.pop('io_loop')
else:
io_loop = ioloop.IOLoop.current()
kwargs['_monitor_class'] = functools.partial(
MotorReplicaSetMonitor, io_loop)
super(MotorReplicaSetClient, self).__init__(io_loop, *args, **kwargs)
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def __init__(self, *args, **kwargs):
"""Create a new connection to a MongoDB replica set.
MotorReplicaSetClient takes the same constructor arguments as
:class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient`,
as well as:
:Parameters:
- `io_loop` (optional): Special :class:`tornado.ioloop.IOLoop`
instance to use instead of default
"""
if 'io_loop' in kwargs:
io_loop = kwargs.pop('io_loop')
else:
io_loop = ioloop.IOLoop.current()
kwargs['_monitor_class'] = functools.partial(
MotorReplicaSetMonitor, io_loop)
super(MotorReplicaSetClient, self).__init__(io_loop, *args, **kwargs)
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def __init__(self, *args, **kwargs):
"""Create a new connection to a single MongoDB instance at *host:port*.
MotorClient takes the same constructor arguments as
:class:`~pymongo.mongo_client.MongoClient`, as well as:
:Parameters:
- `io_loop` (optional): Special :class:`tornado.ioloop.IOLoop`
instance to use instead of default
"""
if 'io_loop' in kwargs:
io_loop = kwargs.pop('io_loop')
else:
io_loop = ioloop.IOLoop.current()
event_class = functools.partial(util.MotorGreenletEvent, io_loop)
kwargs['_event_class'] = event_class
super(MotorClient, self).__init__(io_loop, *args, **kwargs)
def __init__(self, *args, **kwargs):
"""Create a new connection to a MongoDB replica set.
MotorReplicaSetClient takes the same constructor arguments as
:class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient`,
as well as:
:Parameters:
- `io_loop` (optional): Special :class:`tornado.ioloop.IOLoop`
instance to use instead of default
"""
if 'io_loop' in kwargs:
io_loop = kwargs.pop('io_loop')
else:
io_loop = ioloop.IOLoop.current()
kwargs['_monitor_class'] = functools.partial(
MotorReplicaSetMonitor, io_loop)
super(MotorReplicaSetClient, self).__init__(io_loop, *args, **kwargs)
def _run(self):
self._loop = IOLoop()
self._server = FakeAnacondaServer(io_loop=self._loop,
fail_these=self._fail_these,
expected_basename=self._expected_basename)
self._url = self._server.url
def notify_started():
self._started.acquire()
self._started.notify()
self._started.release()
self._loop.add_callback(notify_started)
self._loop.start()
# done
self._server.unlisten()
autoreload.py 文件源码
项目:My-Web-Server-Framework-With-Python2.7
作者: syjsu
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def _testReadWrite(self):
"""
In this test the writer writes an 'x' to its fd. The reader
reads it, check the value and ends the test.
"""
self.shouldWrite = True
def checkReadInput(fd):
self.assertEquals(fd.read(), 'x')
self._reactor.stop()
def writeOnce(fd):
if self.shouldWrite:
self.shouldWrite = False
fd.write('x')
self._reader = Reader(self._p1, checkReadInput)
self._writer = Writer(self._p2, writeOnce)
self._reactor.addWriter(self._writer)
# Test that adding the reader twice adds it only once to
# IOLoop.
self._reactor.addReader(self._reader)
self._reactor.addReader(self._reader)
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def _testReadWrite(self):
"""
In this test the writer writes an 'x' to its fd. The reader
reads it, check the value and ends the test.
"""
self.shouldWrite = True
def checkReadInput(fd):
self.assertEquals(fd.read(), 'x')
self._reactor.stop()
def writeOnce(fd):
if self.shouldWrite:
self.shouldWrite = False
fd.write('x')
self._reader = Reader(self._p1, checkReadInput)
self._writer = Writer(self._p2, writeOnce)
self._reactor.addWriter(self._writer)
# Test that adding the reader twice adds it only once to
# IOLoop.
self._reactor.addReader(self._reader)
self._reactor.addReader(self._reader)
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--serial-port', type=str, required=True)
parser.add_argument('--ws-port', type=int, required=True)
parser.add_argument('--verbose', action='store_true', default=False)
args = parser.parse_args()
loop = IOLoop()
SerialToWs.serial_port = args.serial_port
SerialToWs.verbose = args.verbose
SerialToWs.ioloop = loop
app = Application([
(r'/', SerialToWs)
])
app.listen(args.ws_port)
loop.start()
def connect(host, port):
loop = IOLoop()
Publisher.verbose = False
Publisher.ioloop = loop
app = Application([
(r'/', Publisher)
])
app.listen(port)
url = 'ws://{}:{}'.format(host, port)
if Publisher.verbose:
print('Fake robot serving on {}'.format(url))
t = Thread(target=loop.start)
t.daemon = True
t.start()
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def _run(self):
self._loop = IOLoop()
self._server = FakeAnacondaServer(io_loop=self._loop,
fail_these=self._fail_these,
expected_basename=self._expected_basename)
self._url = self._server.url
def notify_started():
self._started.acquire()
self._started.notify()
self._started.release()
self._loop.add_callback(notify_started)
self._loop.start()
# done
self._server.unlisten()
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def start(io_loop=None, check_time=500):
"""Begins watching source files for changes.
.. versionchanged:: 4.1
The ``io_loop`` argument is deprecated.
"""
io_loop = io_loop or ioloop.IOLoop.current()
if io_loop in _io_loops:
return
_io_loops[io_loop] = True
if len(_io_loops) > 1:
gen_log.warning("tornado.autoreload started more than once in the same process")
if _has_execv:
add_reload_hook(functools.partial(io_loop.close, all_fds=True))
modify_times = {}
callback = functools.partial(_reload_on_update, modify_times)
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
def __init__(self, io_loop, *args, **kwargs):
check_deprecated_kwargs(kwargs)
kwargs['_pool_class'] = functools.partial(MotorPool, io_loop)
kwargs['_connect'] = False
delegate = self.__delegate_class__(*args, **kwargs)
super(MotorClientBase, self).__init__(delegate)
if io_loop:
if not isinstance(io_loop, ioloop.IOLoop):
raise TypeError(
"io_loop must be instance of IOLoop, not %r" % io_loop)
self.io_loop = io_loop
else:
self.io_loop = ioloop.IOLoop.current()
def open(self):
"""Connect to the server.
Takes an optional callback, or returns a Future that resolves to
``self`` when opened. This is convenient for checking at program
startup time whether you can connect.
.. doctest::
>>> client = MotorClient()
>>> # run_sync() returns the open client.
>>> IOLoop.current().run_sync(client.open)
MotorClient(MongoClient('localhost', 27017))
``open`` raises a :exc:`~pymongo.errors.ConnectionFailure` if it
cannot connect, but note that auth failures aren't revealed until
you attempt an operation on the open client.
:Parameters:
- `callback`: Optional function taking parameters (self, error)
.. versionchanged:: 0.2
:class:`MotorClient` now opens itself on demand, calling ``open``
explicitly is now optional.
"""
yield self._ensure_connected()
raise gen.Return(self)