302
303
self.handle, option,
303
304
&optval_int64_c, sizeof(int64_t)
305
elif option in constants.int_sockopts:
307
# default is to assume int, which is what most new sockopts will be
308
# this lets pyzmq work with newer libzmq which may add constants
309
# pyzmq has not yet added, rather than artificially raising. Invalid
310
# sockopts will still raise just the same, but it will be libzmq doing
306
312
if not isinstance(optval, int):
307
313
raise TypeError('expected int, got: %r' % optval)
308
314
optval_int_c = optval
330
The option to set. Available values will depend on your
334
The option to get. Available values will depend on your
331
335
version of libzmq. Examples include:
332
zmq.SUBSCRIBE, UNSUBSCRIBE, IDENTITY, HWM, LINGER, FD
336
zmq.IDENTITY, HWM, LINGER, FD, EVENTS
375
372
result = optval_fd_c
377
raise ZMQError(EINVAL)
374
# default is to assume int, which is what most new sockopts will be
375
# this lets pyzmq work with newer libzmq which may add constants
376
# pyzmq has not yet added, rather than artificially raising. Invalid
377
# sockopts will still raise just the same, but it will be libzmq doing
381
rc = zmq_getsockopt(self.handle, option, <void *>&optval_int_c, &sz)
384
result = optval_int_c
412
option : unicode string
413
The name of the option to set. Can be any of:
414
IDENTITY, HWM, SWAP, AFFINITY, RATE,
415
RECOVERY_IVL, MCAST_LOOP, SNDBUF, RCVBUF, RCVMORE.
420
The option to retrieve. Currently, IDENTITY is the only
421
gettable option that can return a string.
420
426
The value of the option as a unicode string.
423
if option not in [ZMQ_IDENTITY]:
429
if option not in constants.bytes_sockopts:
424
430
raise TypeError("option %i will not return a string to be decoded"%option)
425
431
return self.getsockopt(option).decode(encoding)
664
670
Should the message(s) be tracked for notification that ZMQ has
665
671
finished with it (ignored if copy=True).
666
672
prefix : iterable
667
A sequence of messages to send as a 0MQ label prefix (0MQ >= 3.0 only).
668
Each element can be any sendable object (Message, bytes, buffer-providers)
673
A sequence of messages to send as a 0MQ routing prefix. With the removal
674
of LABELs from libzmq3, `prefix` has no effect beyond being prepended
674
681
a MessageTracker object, whose `pending` property will
675
682
be True until the last send is completed.
677
cdef int SNDLABEL = ZMQ_SNDLABEL
679
685
if isinstance(prefix, bytes):
680
686
prefix = [prefix]
682
# ignore SNDLABEL on early libzmq, as SNDMORE is fine
684
687
for msg in prefix:
685
self.send(msg, SNDLABEL|flags)
688
self.send(msg, SNDMORE|flags)
686
689
for msg in msg_parts[:-1]:
687
690
self.send(msg, SNDMORE|flags, copy=copy, track=track)
688
691
# Send the last part without the extra SNDMORE flag.
713
A list of messages in the multipart message; either Messages or strs,
716
A list of messages in the multipart message; either Messages or bytes,
714
717
depending on `copy`.
718
prefix, msg_parts : two lists
719
`prefix` will be the prefix list of message labels at the front of the
720
message. If prefix would be empty, only a single msg_parts list
725
if ZMQ_VERSION_MAJOR >= 3:
727
# receive the label prefix, if any
728
part = self.recv(flags, copy=copy, track=track)
729
if self.getsockopt(ZMQ_RCVLABEL):
735
# recv the first part
736
part = self.recv(flags, copy=copy, track=track)
720
parts = [self.recv(flags, copy=copy, track=track)]
739
721
# have first part already, only loop while more to receive
740
# LABELS after initial prefix are treated as SNDMORE, and their
741
# LABEL-ness is stripped, but at least complete message is in tact
742
while self.getsockopt(ZMQ_RCVMORE) or \
743
(ZMQ_RCVLABEL != -1 and self.getsockopt(ZMQ_RCVLABEL)):
722
while self.getsockopt(ZMQ_RCVMORE):
744
723
part = self.recv(flags, copy=copy, track=track)
745
724
parts.append(part)
751
728
def send_unicode(self, u, int flags=0, copy=False, encoding='utf-8'):
862
839
msg = self.recv(flags)
863
840
return jsonapi.loads(msg)
842
def poll(self, timeout=None, flags=None):
843
"""s.poll(timeout=None, flags=POLLIN|POLLERR)
845
Poll the socket for events. The default is to poll forever for incoming
846
events. Timeout is in milliseconds, if specified.
850
timeout : int [default: None]
851
The timeout (in milliseconds) to wait for an event. If unspecified
852
(or secified None), will wait forever for an event.
853
flags : bitfield (int) [default: any event]
854
The event flags to poll for (any combination of POLLIN|POLLOUT|POLLERR).
855
The default is to check for incoming events (POLLIN|POLLERR).
859
events : bitfield (int)
860
The events that are ready and waiting. Will be 0 if no events were ready
861
by the time timeout was reached.
864
_check_closed(self, True)
867
flags = POLLIN|POLLERR
869
p.register(self, flags)
870
evts = dict(p.poll(timeout))
871
# return 0 if no events, otherwise return event bitfield
872
return evts.get(self, 0)
866
876
__all__ = ['Socket']