File: connection-objects.txt

package info (click to toggle)
multiprocess 0.70.17-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 9,900 kB
  • sloc: python: 70,095; ansic: 7,509; makefile: 16
file content (112 lines) | stat: -rw-r--r-- 3,774 bytes parent folder | download | duplicates (38)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
.. include:: header.txt

====================
 Connection objects
====================

Connection objects allow the sending and receiving of picklable
objects or strings.  They can be thought of as message oriented
connected sockets.

Connection objects usually created using `processing.Pipe()` -- see
also `Listener and Clients <connection-ref.html>`_.

Connection objects have the following methods:

    `send(obj)`
        Send an object to the other end of the connection which should
        be read using `recv()`.

        The object must be picklable.

    `recv()`
        Return an object sent from the other end of the connection
        using `send()`.  Raises `EOFError` if there is nothing left to
        receive and the other end was closed.

    `fileno()`
        Returns the file descriptor or handle used by the connection.

    `close()`
        Close the connection.

        This is called automatically when the connection is garbage
        collected.

    `poll(timeout=0.0)`
        Return whether there is any data available to be read within
        `timeout` seconds.  

        If `timeout` is `None` then an infinite timeout is used.
        
        Unlike the other blocking methods on Windows this method can
        be interrupted by Ctrl-C.

    `sendBytes(buffer)`
        Send byte data from an object supporting the buffer interface
        as a complete message.

        Can be used to send strings or a view returned by `buffer()`.

    `recvBytes()` 
        Return a complete message of byte data sent from the other end
        of the connection as a string.  Raises `EOFError` if there is
        nothing left to receive and the other end was closed.

    `recvBytesInto(buffer, offset=0)`
        Read into `buffer` at position `offset` a complete message of
        byte data sent from the other end of the connection and return
        the number of bytes in the message.  Raises `EOFError` if
        there is nothing left to receive and the other end was closed.

        `buffer` must be an object satisfying the writable buffer
        interface and `offset` must be non-negative and less than
        the length of `buffer` (in bytes).
    
        If the buffer is too short then a `BufferTooShort` exception
        is raised and the complete message is available as `e.args[0]`
        where `e` is the exception instance.


For example:

    >>> from processing import Pipe
    >>> a, b = Pipe()
    >>> a.send([1, 'hello', None])
    >>> b.recv()
    [1, 'hello', None]
    >>> b.sendBytes('thank you')
    >>> a.recvBytes()
    'thank you'
    >>> import array
    >>> arr1 = array.array('i', range(5))
    >>> arr2 = array.array('i', [0] * 10)
    >>> a.sendBytes(arr1)
    >>> count = b.recvBytesInto(arr2)
    >>> assert count == len(arr1) * arr1.itemsize
    >>> arr2
    array('i', [0, 1, 2, 3, 4, 0, 0, 0, 0, 0])


.. warning::
    
    The `recv()` method automatically unpickles the data it receives
    which can be a security risk unless you can trust the process
    which sent the message.
    
    Therefore, unless the connection object was produced using
    `Pipe()` you should only use the `recv()` and `send()` methods
    after performing some sort of authentication.  See `Authentication
    keys <connection-ref.html#authentication-keys>`_.
    
    
.. warning::
    
    If a process is killed while it is trying to read or write to a
    pipe then the data in the pipe is likely to become corrupted
    because it may become impossible to be sure where the message
    boundaries lie.

.. _Prev: queue-objects.html
.. _Up: processing-ref.html
.. _Next: manager-objects.html