本文整理汇总了Python中gevent.hub.get_hub方法的典型用法代码示例。如果您正苦于以下问题:Python hub.get_hub方法的具体用法?Python hub.get_hub怎么用?Python hub.get_hub使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.hub
的用法示例。
在下文中一共展示了hub.get_hub方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def __init__(self, value=1):
if value < 0:
raise ValueError("semaphore initial value must be >= 0")
self.counter = value
self._dirty = False
# In PyPy 2.6.1 with Cython 0.23, `cdef public` or `cdef
# readonly` or simply `cdef` attributes of type `object` can appear to leak if
# a Python subclass is used (this is visible simply
# instantiating this subclass if _links=[]). Our _links and
# _notifier are such attributes, and gevent.thread subclasses
# this class. Thus, we carefully manage the lifetime of the
# objects we put in these attributes so that, in the normal
# case of a semaphore used correctly (deallocated when it's not
# locked and no one is waiting), the leak goes away (because
# these objects are back to None). This can also be solved on PyPy
# by simply not declaring these objects in the pxd file, but that doesn't work for
# CPython ("No attribute...")
# See https://github.com/gevent/gevent/issues/660
self._links = None
self._notifier = None
# we don't want to do get_hub() here to allow defining module-level locks
# without initializing the hub
示例2: _do_wait
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def _do_wait(self, timeout):
"""
Wait for up to *timeout* seconds to expire. If timeout
elapses, return the exception. Otherwise, return None.
Raises timeout if a different timer expires.
"""
switch = getcurrent().switch
self.rawlink(switch)
try:
timer = Timeout._start_new_or_dummy(timeout)
try:
try:
result = get_hub().switch()
assert result is self, 'Invalid switch into Semaphore.wait/acquire(): %r' % (result, )
except Timeout as ex:
if ex is not timer:
raise
return ex
finally:
timer.cancel()
finally:
self.unlink(switch)
示例3: __init__
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def __init__(self, hub=None, use_environ=True, **kwargs):
if hub is None:
hub = get_hub()
self.hub = hub
if use_environ:
for key in os.environ:
if key.startswith('GEVENTARES_'):
name = key[11:].lower()
if name:
value = os.environ[key]
kwargs.setdefault(name, value)
self.ares = self.ares_class(hub.loop, **kwargs)
self.pid = os.getpid()
self.params = kwargs
self.fork_watcher = hub.loop.fork(ref=False)
self.fork_watcher.start(self._on_fork)
示例4: nb_read
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def nb_read(fd, n):
"""Read up to `n` bytes from file descriptor `fd`. Return a string
containing the bytes read. If end-of-file is reached, an empty string
is returned.
The descriptor must be in non-blocking mode.
"""
hub, event = None, None
while True:
try:
return _read(fd, n)
except OSError as e:
if e.errno not in ignored_errors:
raise
if not PY3:
sys.exc_clear()
if hub is None:
hub = get_hub()
event = hub.loop.io(fd, 1)
hub.wait(event)
示例5: nb_write
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def nb_write(fd, buf):
"""Write bytes from buffer `buf` to file descriptor `fd`. Return the
number of bytes written.
The file descriptor must be in non-blocking mode.
"""
hub, event = None, None
while True:
try:
return _write(fd, buf)
except OSError as e:
if e.errno not in ignored_errors:
raise
if not PY3:
sys.exc_clear()
if hub is None:
hub = get_hub()
event = hub.loop.io(fd, 2)
hub.wait(event)
示例6: __init__
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def __init__(self, listener, handle=None, spawn='default'):
self._stop_event = Event()
self._stop_event.set()
self._watcher = None
self._timer = None
self.pool = None
try:
self.set_listener(listener)
self.set_spawn(spawn)
self.set_handle(handle)
self.delay = self.min_delay
self.loop = get_hub().loop
if self.max_accept < 1:
raise ValueError('max_accept must be positive int: %r' % (self.max_accept, ))
except:
self.close()
raise
示例7: __init__
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def __init__(self, fileno, mode='r', closefd=True):
RawIOBase.__init__(self)
self._closed = False
self._closefd = closefd
self._fileno = fileno
make_nonblocking(fileno)
self._readable = 'r' in mode
self._writable = 'w' in mode
self.hub = get_hub()
io = self.hub.loop.io
if self._readable:
self._read_event = io(fileno, 1)
else:
self._read_event = None
if self._writable:
self._write_event = io(fileno, 2)
else:
self._write_event = None
self._seekable = None
示例8: getaddrinfo
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
"""
Resolve host and port into list of address info entries.
Translate the host/port argument into a sequence of 5-tuples that contain
all the necessary arguments for creating a socket connected to that service.
host is a domain name, a string representation of an IPv4/v6 address or
None. port is a string service name such as 'http', a numeric port number or
None. By passing None as the value of host and port, you can pass NULL to
the underlying C API.
The family, type and proto arguments can be optionally specified in order to
narrow the list of addresses returned. Passing zero as a value for each of
these arguments selects the full range of results.
.. seealso:: :doc:`dns`
"""
return get_hub().resolver.getaddrinfo(host, port, family, socktype, proto, flags)
示例9: greenlet_callback
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def greenlet_callback(event, args):
"""
This is a callback that is executed greenlet on all events.
"""
if event in ("switch", "throw"):
# It's only safe to unpack args under these two events.
(origin, _target) = args
if origin is get_hub():
# This greenlet is the one that manages the loop itself, thus noop.
return
if event == "switch":
switch_callback(args)
return
if event == "throw":
throw_callback(args)
return
示例10: __init__
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def __init__(self, listener, handle=None, spawn='default'):
self._stop_event = Event()
self._stop_event.set()
self._watcher = None
self._timer = None
self._handle = None
# XXX: FIXME: Subclasses rely on the presence or absence of the
# `socket` attribute to determine whether we are open/should be opened.
# Instead, have it be None.
self.pool = None
try:
self.set_listener(listener)
self.set_spawn(spawn)
self.set_handle(handle)
self.delay = self.min_delay
self.loop = get_hub().loop
if self.max_accept < 1:
raise ValueError('max_accept must be positive int: %r' % (self.max_accept, ))
except:
self.close()
raise
示例11: __init__
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def __init__(self, fileno, mode='r', closefd=True):
RawIOBase.__init__(self) # Python 2: pylint:disable=no-member,non-parent-init-called
self._closed = False
self._closefd = closefd
self._fileno = fileno
make_nonblocking(fileno)
self._readable = 'r' in mode
self._writable = 'w' in mode
self.hub = get_hub()
io_watcher = self.hub.loop.io
if self._readable:
self._read_event = io_watcher(fileno, 1)
if self._writable:
self._write_event = io_watcher(fileno, 2)
self._seekable = None
示例12: __init__
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def __init__(self, hub=None, **kwargs):
if hub is None:
hub = get_hub()
self.hub = hub
self.ares = self.ares_class(hub.loop, **kwargs)
self.pid = os.getpid()
self.params = kwargs
self.fork_watcher = hub.loop.fork(ref=False)
self.fork_watcher.start(self._on_fork)
示例13: __setup_events
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def __setup_events(self):
self.__readable = AsyncResult()
self.__writable = AsyncResult()
self.__readable.set()
self.__writable.set()
try:
self._state_event = get_hub().loop.io(self.getsockopt(zmq.FD), 1) # read state watcher
self._state_event.start(self.__state_changed)
except AttributeError:
# for gevent<1.0 compatibility
from gevent.core import read_event
self._state_event = read_event(self.getsockopt(zmq.FD), self.__state_changed, persist=True)
示例14: _start_notify
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def _start_notify(self):
if self._links and self.counter > 0 and not self._notifier:
# We create a new self._notifier each time through the loop,
# if needed. (it has a __bool__ method that tells whether it has
# been run; once it's run once---at the end of the loop---it becomes
# false.)
# NOTE: Passing the bound method will cause a memory leak on PyPy
# with Cython <= 0.23.3. You must use >= 0.23.4.
# See https://bitbucket.org/pypy/pypy/issues/2149/memory-leak-for-python-subclass-of-cpyext#comment-22371546
self._notifier = get_hub().loop.run_callback(self._notify_links)
示例15: __init__
# 需要导入模块: from gevent import hub [as 别名]
# 或者: from gevent.hub import get_hub [as 别名]
def __init__(self, hub=None):
if hub is None:
hub = get_hub()
self.pool = hub.threadpool
if _socket.gaierror not in hub.NOT_ERROR:
# Do not cause lookup failures to get printed by the default
# error handler. This can be very noisy.
hub.NOT_ERROR += (_socket.gaierror, _socket.herror)