本文整理汇总了Python中zmq.EVENTS属性的典型用法代码示例。如果您正苦于以下问题:Python zmq.EVENTS属性的具体用法?Python zmq.EVENTS怎么用?Python zmq.EVENTS使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类zmq
的用法示例。
在下文中一共展示了zmq.EVENTS属性的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _notify_stream_qt
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _notify_stream_qt(kernel, stream):
from IPython.external.qt_for_kernel import QtCore
if _on_os_x_10_9() and kernel._darwin_app_nap:
from IPython.external.appnope import nope_scope as context
else:
from IPython.core.interactiveshell import NoOpContext as context
def process_stream_events():
while stream.getsockopt(zmq.EVENTS) & zmq.POLLIN:
with context():
kernel.do_one_iteration()
fd = stream.getsockopt(zmq.FD)
notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app)
notifier.activated.connect(process_stream_events)
示例2: step
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def step(self):
''' Perform one iteration step. Reimplemented in Simulation. '''
# Process timers
Timer.update_timers()
# Get new events from the I/O thread
# while self.event_io.poll(0):
if self.event_io.getsockopt(zmq.EVENTS) & zmq.POLLIN:
msg = self.event_io.recv_multipart()
route, eventname, data = msg[:-2], msg[-2], msg[-1]
# route back to sender is acquired by reversing the incoming route
route.reverse()
if eventname == b'QUIT':
self.quit()
else:
pydata = msgpack.unpackb(
data, object_hook=decode_ndarray, raw=False)
bs.sim.event(eventname, pydata, route)
示例3: get
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def get(self, key):
result = super(_AsyncSocket, self).get(key)
if key == _zmq.EVENTS:
self._schedule_remaining_events(result)
return result
示例4: _add_recv_event
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _add_recv_event(self, kind, kwargs=None, future=None):
"""Add a recv event, returning the corresponding Future"""
f = future or self._Future()
if kind.startswith('recv') and kwargs.get('flags', 0) & _zmq.DONTWAIT:
# short-circuit non-blocking calls
recv = getattr(self._shadow_sock, kind)
try:
r = recv(**kwargs)
except Exception as e:
f.set_exception(e)
else:
f.set_result(r)
return f
# we add it to the list of futures before we add the timeout as the
# timeout will remove the future from recv_futures to avoid leaks
self._recv_futures.append(
_FutureEvent(f, kind, kwargs, msg=None)
)
# Don't let the Future sit in _recv_events after it's done
f.add_done_callback(lambda f: self._remove_finished_future(f, self._recv_futures))
if hasattr(_zmq, 'RCVTIMEO'):
timeout_ms = self._shadow_sock.rcvtimeo
if timeout_ms >= 0:
self._add_timeout(f, timeout_ms * 1e-3)
if self._shadow_sock.EVENTS & POLLIN:
# recv immediately, if we can
self._handle_recv()
if self._recv_futures:
self._add_io_state(POLLIN)
return f
示例5: _add_send_event
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _add_send_event(self, kind, msg=None, kwargs=None, future=None):
"""Add a send event, returning the corresponding Future"""
f = future or self._Future()
if kind.startswith('send') and kwargs.get('flags', 0) & _zmq.DONTWAIT:
# short-circuit non-blocking calls
send = getattr(self._shadow_sock, kind)
try:
r = send(msg, **kwargs)
except Exception as e:
f.set_exception(e)
else:
f.set_result(r)
return f
# we add it to the list of futures before we add the timeout as the
# timeout will remove the future from recv_futures to avoid leaks
self._send_futures.append(
_FutureEvent(f, kind, kwargs=kwargs, msg=msg)
)
# Don't let the Future sit in _send_futures after it's done
f.add_done_callback(lambda f: self._remove_finished_future(f, self._send_futures))
if hasattr(_zmq, 'SNDTIMEO'):
timeout_ms = self._shadow_sock.sndtimeo
if timeout_ms >= 0:
self._add_timeout(f, timeout_ms * 1e-3)
if self._shadow_sock.EVENTS & POLLOUT:
# send immediately if we can
self._handle_send()
if self._send_futures:
self._add_io_state(POLLOUT)
return f
示例6: _handle_recv
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _handle_recv(self):
"""Handle recv events"""
if not self._shadow_sock.EVENTS & POLLIN:
# event triggered, but state may have been changed between trigger and callback
return
f = None
while self._recv_futures:
f, kind, kwargs, _ = self._recv_futures.popleft()
f._pyzmq_popped = True
# skip any cancelled futures
if f.done():
f = None
else:
break
if not self._recv_futures:
self._drop_io_state(POLLIN)
if f is None:
return
if kind == 'poll':
# on poll event, just signal ready, nothing else.
f.set_result(None)
return
elif kind == 'recv_multipart':
recv = self._shadow_sock.recv_multipart
elif kind == 'recv':
recv = self._shadow_sock.recv
else:
raise ValueError("Unhandled recv event type: %r" % kind)
kwargs['flags'] |= _zmq.DONTWAIT
try:
result = recv(**kwargs)
except Exception as e:
f.set_exception(e)
else:
f.set_result(result)
示例7: _handle_send
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _handle_send(self):
if not self._shadow_sock.EVENTS & POLLOUT:
# event triggered, but state may have been changed between trigger and callback
return
f = None
while self._send_futures:
f, kind, kwargs, msg = self._send_futures.popleft()
f._pyzmq_popped = True
# skip any cancelled futures
if f.done():
f = None
else:
break
if not self._send_futures:
self._drop_io_state(POLLOUT)
if f is None:
return
if kind == 'poll':
# on poll event, just signal ready, nothing else.
f.set_result(None)
return
elif kind == 'send_multipart':
send = self._shadow_sock.send_multipart
elif kind == 'send':
send = self._shadow_sock.send
else:
raise ValueError("Unhandled send event type: %r" % kind)
kwargs['flags'] |= _zmq.DONTWAIT
try:
result = send(msg, **kwargs)
except Exception as e:
f.set_exception(e)
else:
f.set_result(result)
# event masking from ZMQStream
示例8: _schedule_remaining_events
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _schedule_remaining_events(self, events=None):
"""Schedule a call to handle_events next loop iteration
If there are still events to handle.
"""
# edge-triggered handling
# allow passing events in, in case this is triggered by retrieving events,
# so we don't have to retrieve it twice.
if events is None:
events = self._shadow_sock.EVENTS
if events & self._state:
self._call_later(0, self._handle_events)
示例9: __state_changed
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def __state_changed(self, event=None, _evtype=None):
if self.closed:
self.__cleanup_events()
return
try:
# avoid triggering __state_changed from inside __state_changed
events = super(_Socket, self).getsockopt(zmq.EVENTS)
except zmq.ZMQError as exc:
self.__writable.set_exception(exc)
self.__readable.set_exception(exc)
else:
if events & zmq.POLLOUT:
self.__writable.set()
if events & zmq.POLLIN:
self.__readable.set()
示例10: _wait_write
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _wait_write(self):
assert self.__writable.ready(), "Only one greenlet can be waiting on this event"
self.__writable = AsyncResult()
# timeout is because libzmq cannot be trusted to properly signal a new send event:
# this is effectively a maximum poll interval of 1s
tic = time.time()
dt = self._gevent_bug_timeout
if dt:
timeout = gevent.Timeout(seconds=dt)
else:
timeout = None
try:
if timeout:
timeout.start()
self.__writable.get(block=True)
except gevent.Timeout as t:
if t is not timeout:
raise
toc = time.time()
# gevent bug: get can raise timeout even on clean return
# don't display zmq bug warning for gevent bug (this is getting ridiculous)
if self._debug_gevent and timeout and toc-tic > dt and \
self.getsockopt(zmq.EVENTS) & zmq.POLLOUT:
print("BUG: gevent may have missed a libzmq send event on %i!" % self.FD, file=sys.stderr)
finally:
if timeout:
timeout.cancel()
self.__writable.set()
示例11: _wait_read
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _wait_read(self):
assert self.__readable.ready(), "Only one greenlet can be waiting on this event"
self.__readable = AsyncResult()
# timeout is because libzmq cannot always be trusted to play nice with libevent.
# I can only confirm that this actually happens for send, but lets be symmetrical
# with our dirty hacks.
# this is effectively a maximum poll interval of 1s
tic = time.time()
dt = self._gevent_bug_timeout
if dt:
timeout = gevent.Timeout(seconds=dt)
else:
timeout = None
try:
if timeout:
timeout.start()
self.__readable.get(block=True)
except gevent.Timeout as t:
if t is not timeout:
raise
toc = time.time()
# gevent bug: get can raise timeout even on clean return
# don't display zmq bug warning for gevent bug (this is getting ridiculous)
if self._debug_gevent and timeout and toc-tic > dt and \
self.getsockopt(zmq.EVENTS) & zmq.POLLIN:
print("BUG: gevent may have missed a libzmq recv event on %i!" % self.FD, file=sys.stderr)
finally:
if timeout:
timeout.cancel()
self.__readable.set()
示例12: get
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def get(self, opt):
"""trigger state_changed on getsockopt(EVENTS)"""
if opt in TIMEOS:
warnings.warn("TIMEO socket options have no effect in zmq.green", UserWarning)
optval = super(_Socket, self).get(opt)
if opt == zmq.EVENTS:
self.__state_changed()
return optval
示例13: _add_send_event
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _add_send_event(self, kind, msg=None, kwargs=None, future=None):
"""Add a send event, returning the corresponding Future"""
f = future or self._Future()
if kind.startswith('send') and kwargs.get('flags', 0) & _zmq.DONTWAIT:
# short-circuit non-blocking calls
send = getattr(self._shadow_sock, kind)
try:
r = send(msg, **kwargs)
except Exception as e:
f.set_exception(e)
else:
f.set_result(r)
return f
# we add it to the list of futures before we add the timeout as the
# timeout will remove the future from recv_futures to avoid leaks
self._send_futures.append(
_FutureEvent(f, kind, kwargs=kwargs, msg=msg)
)
# Don't let the Future sit in _send_futures after it's done
f.add_done_callback(lambda f: self._remove_finished_future(f, self._send_futures))
if hasattr(_zmq, 'SNDTIMEO'):
timeout_ms = self._shadow_sock.sndtimeo
if timeout_ms >= 0:
self._add_timeout(f, timeout_ms * 1e-3)
if self._shadow_sock.EVENTS & POLLOUT:
# send immediately if we can
self._handle_send()
# make sure we schedule pending events
# if we are taking this shortcut
# only if not _send_futures because `_add_io_state`
# does the same thing below
if not self._send_futures:
self._schedule_remaining_events()
if self._send_futures:
self._add_io_state(POLLOUT)
return f
示例14: _handle_recv
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import EVENTS [as 别名]
def _handle_recv(self):
"""Handle recv events"""
if not self._shadow_sock.EVENTS & POLLIN:
# event triggered, but state may have been changed between trigger and callback
return
f = None
while self._recv_futures:
f, kind, kwargs, _ = self._recv_futures.popleft()
f._pyzmq_popped = True
# skip any cancelled futures
if f.done():
f = None
else:
break
if not self._recv_futures:
self._drop_io_state(POLLIN)
if f is None:
return
if kind == 'poll':
# on poll event, just signal ready, nothing else.
f.set_result(None)
return
elif kind == 'recv_multipart':
recv = self._shadow_sock.recv_multipart
elif kind == 'recv':
recv = self._shadow_sock.recv
else:
raise ValueError("Unhandled recv event type: %r" % kind)
kwargs['flags'] |= _zmq.DONTWAIT
try:
result = recv(**kwargs)
except Exception as e:
f.set_exception(e)
else:
f.set_result(result)