本文整理匯總了Python中gevent.select方法的典型用法代碼示例。如果您正苦於以下問題:Python gevent.select方法的具體用法?Python gevent.select怎麽用?Python gevent.select使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gevent
的用法示例。
在下文中一共展示了gevent.select方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import select [as 別名]
def __init__(self, conn, key, n_replicas=RING_REPLICAS):
"""
Initializes a Redis hash ring node, given the Redis connection, a key
and the number of replicas.
"""
self.conn = conn
self.key = key
host = socket.gethostname()
pid = os.getpid()
# Create unique identifiers for the replicas
self.replicas = [(
random.randrange(2**32),
'{host}:{pid}:{rand}'.format(
host=host,
pid=pid,
rand=binascii.hexlify(os.urandom(4)).decode()
)
) for n in range(n_replicas)]
# List of tuples of ranges this node is responsible for, where a tuple
# (a, b) includes any N matching a <= N < b.
self.ranges = []
self._select = select.select
示例2: gevent_start
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import select [as 別名]
def gevent_start(self):
"""
Helper method to start the node for gevent-based applications.
"""
import gevent
import gevent.select
self._poller_greenlet = gevent.spawn(self.poll)
self._select = gevent.select.select
self.heartbeat()
self.update()
示例3: gevent_stop
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import select [as 別名]
def gevent_stop(self):
"""
Helper method to stop the node for gevent-based applications.
"""
import gevent
gevent.kill(self._poller_greenlet)
self.remove()
self._select = select.select
示例4: __call__
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import select [as 別名]
def __call__(self, environ, start_response):
self._sock = uwsgi.connection_fd()
self.environ = environ
uwsgi.websocket_handshake()
self._req_ctx = None
if hasattr(uwsgi, 'request_context'):
# uWSGI >= 2.1.x with support for api access across-greenlets
self._req_ctx = uwsgi.request_context()
else:
# use event and queue for sending messages
from gevent.event import Event
from gevent.queue import Queue
from gevent.select import select
self._event = Event()
self._send_queue = Queue()
# spawn a select greenlet
def select_greenlet_runner(fd, event):
"""Sets event when data becomes available to read on fd."""
while True:
event.set()
try:
select([fd], [], [])[0]
except ValueError:
break
self._select_greenlet = gevent.spawn(
select_greenlet_runner,
self._sock,
self._event)
self.app(self)
示例5: poll
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import select [as 別名]
def poll(self):
"""
Main loop which maintains the node in the hash ring. Can be run in a
greenlet or separate thread. This takes care of:
* Updating the heartbeat
* Checking for ring updates
* Cleaning up expired nodes periodically
"""
pubsub = self.conn.pubsub()
pubsub.subscribe(self.key)
# Pubsub messages generator
gen = pubsub.listen()
last_heartbeat = time.time()
self.heartbeat()
last_cleanup = time.time()
self.cleanup()
try:
while True:
# Since Redis' listen method blocks, we use select to inspect the
# underlying socket to see if there is activity.
fileno = pubsub.connection._sock.fileno()
timeout = max(0, POLL_INTERVAL - (time.time() - last_heartbeat))
r, w, x = self._select([fileno], [], [], timeout)
if fileno in r:
next(gen)
self.update()
last_heartbeat = time.time()
self.heartbeat()
now = time.time()
if now - last_cleanup > CLEANUP_INTERVAL:
last_cleanup = now
self.cleanup()
finally:
pubsub.close()
示例6: run
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import select [as 別名]
def run(self):
# FIXME(richo) Mutex around shared mutable state
seen = self.conn.seen
while True:
try:
select([self.conn.central.stack], [], [])
except native_select.error as ex:
if ex[0] == errno.EINTR:
continue
raise
event = self.conn.central.stack.handle_data()
if event.type == BTEvent.SCAN_DATA:
addr, type, data = event.data
print ("Saw %s (%s)" %
(addr, "public" if type == 0 else "random"))
if addr in seen:
if len(data) > len(seen[addr][1]):
seen[addr] = (type, data)
self.dump_gap(data)
else:
seen[addr] = (type, data)
self.dump_gap(data)
elif event.type == BTEvent.CONNECTED:
# FIXME(richo) Mutex
self.conn.connected = True
print "Connected!"
if len(self.conn.onconnect) > 0:
print "Running onconnect comands"
while self.conn.onconnect():
cmd = self.conn.onconnect(0)
cmd()
elif event.type == BTEvent.DISCONNECTED:
self.conn.connected = False
print "Disconnected"
elif event.type == BTEvent.ATT_DATA:
pkt = event.data
# ack handle value notification
if pkt.opcode == 0x1d:
self.central.stack.raw_att("\x1e")
print event
elif event.type != BTEvent.NONE:
print event