~ubuntu-branches/ubuntu/precise/pyzmq/precise

« back to all changes in this revision

Viewing changes to zmq/core/socket.pyx

  • Committer: Package Import Robot
  • Author(s): Julian Taylor, Julian Taylor
  • Date: 2012-02-19 14:53:20 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20120219145320-u8un3j0q1e82dx01
[ Julian Taylor ]
* New upstream release (Closes: #655793)
* added myself to Uploaders
* properly clean to allow building twice in a row
* ignore testsuite result due to random failures on armel and mips
  probably caused by libzmq or toolchain
* add Python3 to short description of py3 packages
* wrap-and-sort debian/
* add python-nose to build depends to get better test result output
* add python-numpy to build depends to get more tests
* use githubredir.debian.net in debian/watch

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
    cPickle = None
62
62
    import pickle
63
63
 
 
64
import zmq
64
65
from zmq.core import constants
65
66
from zmq.core.constants import *
66
67
from zmq.core.error import ZMQError, ZMQBindError
302
303
                    self.handle, option,
303
304
                    &optval_int64_c, sizeof(int64_t)
304
305
                )
305
 
        elif option in constants.int_sockopts:
 
306
        else:
 
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
 
311
            # the raising.
306
312
            if not isinstance(optval, int):
307
313
                raise TypeError('expected int, got: %r' % optval)
308
314
            optval_int_c = optval
311
317
                    self.handle, option,
312
318
                    &optval_int_c, sizeof(int)
313
319
                )
314
 
        else:
315
 
            raise ZMQError(EINVAL)
316
320
 
317
321
        if rc != 0:
318
322
            raise ZMQError()
327
331
        Parameters
328
332
        ----------
329
333
        option : int
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
333
337
 
334
338
        Returns
335
339
        -------
359
363
            if rc != 0:
360
364
                raise ZMQError()
361
365
            result = optval_int64_c
362
 
        elif option in constants.int_sockopts:
363
 
            sz = sizeof(int)
364
 
            with nogil:
365
 
                rc = zmq_getsockopt(self.handle, option, <void *>&optval_int_c, &sz)
366
 
            if rc != 0:
367
 
                raise ZMQError()
368
 
            result = optval_int_c
369
366
        elif option == ZMQ_FD:
370
367
            sz = sizeof(fd_t)
371
368
            with nogil:
374
371
                raise ZMQError()
375
372
            result = optval_fd_c
376
373
        else:
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
 
378
            # the raising.
 
379
            sz = sizeof(int)
 
380
            with nogil:
 
381
                rc = zmq_getsockopt(self.handle, option, <void *>&optval_int_c, &sz)
 
382
            if rc != 0:
 
383
                raise ZMQError()
 
384
            result = optval_int_c
378
385
 
379
386
        return result
380
387
    
409
416
 
410
417
        Parameters
411
418
        ----------
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.
 
419
        option : int
 
420
            The option to retrieve. Currently, IDENTITY is the only
 
421
            gettable option that can return a string.
416
422
 
417
423
        Returns
418
424
        -------
420
426
            The value of the option as a unicode string.
421
427
        """
422
428
        
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)
426
432
    
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
 
675
            to msg_parts.
669
676
        
670
677
        Returns
671
678
        -------
674
681
            a MessageTracker object, whose `pending` property will
675
682
            be True until the last send is completed.
676
683
        """
677
 
        cdef int SNDLABEL = ZMQ_SNDLABEL
678
684
        if prefix:
679
685
            if isinstance(prefix, bytes):
680
686
                prefix = [prefix]
681
 
            if SNDLABEL == -1:
682
 
                # ignore SNDLABEL on early libzmq, as SNDMORE is fine
683
 
                SNDLABEL = SNDMORE
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.
710
713
        Returns
711
714
        -------
712
715
        msg_parts : list
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`.
715
718
        
716
 
        0MQ-3.0:
717
 
        
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
721
 
            will be returned.
722
719
        """
723
 
        parts = []
724
 
        prefix = []
725
 
        if ZMQ_VERSION_MAJOR >= 3:
726
 
            while True:
727
 
                # receive the label prefix, if any
728
 
                part = self.recv(flags, copy=copy, track=track)
729
 
                if self.getsockopt(ZMQ_RCVLABEL):
730
 
                    prefix.append(part)
731
 
                else:
732
 
                    parts.append(part)
733
 
                    break
734
 
        else:
735
 
            # recv the first part
736
 
            part = self.recv(flags, copy=copy, track=track)
737
 
            parts.append(part)
738
 
            
 
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)
746
725
        
747
 
        if prefix:
748
 
            return prefix,parts
749
726
        return parts
750
727
 
751
728
    def send_unicode(self, u, int flags=0, copy=False, encoding='utf-8'):
861
838
        else:
862
839
            msg = self.recv(flags)
863
840
            return jsonapi.loads(msg)
 
841
    
 
842
    def poll(self, timeout=None, flags=None):
 
843
        """s.poll(timeout=None, flags=POLLIN|POLLERR)
 
844
        
 
845
        Poll the socket for events.  The default is to poll forever for incoming
 
846
        events.  Timeout is in milliseconds, if specified.
 
847
        
 
848
        Parameters
 
849
        ----------
 
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).
 
856
        
 
857
        Returns
 
858
        -------
 
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.
 
862
        """
 
863
        
 
864
        _check_closed(self, True)
 
865
        
 
866
        if flags is None:
 
867
            flags = POLLIN|POLLERR
 
868
        p = zmq.Poller()
 
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)
 
873
        
864
874
 
865
875
 
866
876
__all__ = ['Socket']