|
5 | 5 | ----------
|
6 | 6 | 问题
|
7 | 7 | ----------
|
8 |
| -You want to send and receive large arrays of contiguous data across a network connec‐ |
9 |
| -tion, making as few copies of the data as possible. |
| 8 | +你要通过网络连接发送和接受连续数据的大型数组,并尽量减少数据的复制操作。 |
10 | 9 |
|
11 | 10 | |
|
12 | 11 |
|
13 | 12 | ----------
|
14 | 13 | 解决方案
|
15 | 14 | ----------
|
16 |
| -The following functions utilize memoryviews to send and receive large arrays: |
| 15 | +下面的函数利用 ``memoryviews`` 来发送和接受大数组: |
17 | 16 |
|
18 |
| -# zerocopy.py |
| 17 | +.. code-block:: python |
19 | 18 |
|
20 |
| -def send_from(arr, dest): |
21 |
| - view = memoryview(arr).cast('B') |
22 |
| - while len(view): |
23 |
| - nsent = dest.send(view) |
24 |
| - view = view[nsent:] |
| 19 | + # zerocopy.py |
25 | 20 |
|
26 |
| -def recv_into(arr, source): |
27 |
| - view = memoryview(arr).cast('B') |
28 |
| - while len(view): |
29 |
| - nrecv = source.recv_into(view) |
30 |
| - view = view[nrecv:] |
31 |
| - |
32 |
| -To test the program, first create a server and client program connected over a socket. |
33 |
| -In the server: |
34 |
| - |
35 |
| ->>> from socket import * |
36 |
| ->>> s = socket(AF_INET, SOCK_STREAM) |
37 |
| ->>> s.bind(('', 25000)) |
38 |
| ->>> s.listen(1) |
39 |
| ->>> c,a = s.accept() |
40 |
| ->>> |
41 |
| - |
42 |
| -In the client (in a separate interpreter): |
43 |
| - |
44 |
| ->>> from socket import * |
45 |
| ->>> c = socket(AF_INET, SOCK_STREAM) |
46 |
| ->>> c.connect(('localhost', 25000)) |
47 |
| ->>> |
48 |
| - |
49 |
| -Now, the whole idea of this recipe is that you can blast a huge array through the con‐ |
50 |
| -nection. In this case, arrays might be created by the array module or perhaps numpy. |
51 |
| -For example: |
52 |
| -# Server |
53 |
| ->>> import numpy |
54 |
| ->>> a = numpy.arange(0.0, 50000000.0) |
55 |
| ->>> send_from(a, c) |
56 |
| ->>> |
57 |
| - |
58 |
| -# Client |
59 |
| ->>> import numpy |
60 |
| ->>> a = numpy.zeros(shape=50000000, dtype=float) |
61 |
| ->>> a[0:10] |
62 |
| -array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) |
63 |
| ->>> recv_into(a, c) |
64 |
| ->>> a[0:10] |
65 |
| -array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) |
66 |
| ->>> |
| 21 | + def send_from(arr, dest): |
| 22 | + view = memoryview(arr).cast('B') |
| 23 | + while len(view): |
| 24 | + nsent = dest.send(view) |
| 25 | + view = view[nsent:] |
| 26 | +
|
| 27 | + def recv_into(arr, source): |
| 28 | + view = memoryview(arr).cast('B') |
| 29 | + while len(view): |
| 30 | + nrecv = source.recv_into(view) |
| 31 | + view = view[nrecv:] |
| 32 | +
|
| 33 | +为了测试程序,首先创建一个通过socket连接的服务器和客户端程序: |
| 34 | + |
| 35 | +.. code-block:: python |
| 36 | +
|
| 37 | + >>> from socket import * |
| 38 | + >>> s = socket(AF_INET, SOCK_STREAM) |
| 39 | + >>> s.bind(('', 25000)) |
| 40 | + >>> s.listen(1) |
| 41 | + >>> c,a = s.accept() |
| 42 | + >>> |
| 43 | +
|
| 44 | +在客户端(另外一个解释器中): |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + >>> from socket import * |
| 49 | + >>> c = socket(AF_INET, SOCK_STREAM) |
| 50 | + >>> c.connect(('localhost', 25000)) |
| 51 | + >>> |
| 52 | +
|
| 53 | +本节的目标是你能通过连接传输一个超大数组。这种情况的话,可以通过 ``array`` 模块或 ``numpy`` 模块来创建数组: |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | +
|
| 57 | + # Server |
| 58 | + >>> import numpy |
| 59 | + >>> a = numpy.arange(0.0, 50000000.0) |
| 60 | + >>> send_from(a, c) |
| 61 | + >>> |
| 62 | +
|
| 63 | + # Client |
| 64 | + >>> import numpy |
| 65 | + >>> a = numpy.zeros(shape=50000000, dtype=float) |
| 66 | + >>> a[0:10] |
| 67 | + array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) |
| 68 | + >>> recv_into(a, c) |
| 69 | + >>> a[0:10] |
| 70 | + array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) |
| 71 | + >>> |
67 | 72 |
|
68 | 73 | |
|
69 | 74 |
|
70 | 75 | ----------
|
71 | 76 | 讨论
|
72 | 77 | ----------
|
73 |
| -In data-intensive distributed computing and parallel programming applications, it’s not |
74 |
| -uncommon to write programs that need to send/receive large chunks of data. However, |
75 |
| -to do this, you somehow need to reduce the data down to raw bytes for use with low- |
76 |
| -level network functions. You may also need to slice the data into chunks, since most |
77 |
| -network-related functions aren’t able to send or receive huge blocks of data entirely all |
78 |
| -at once. |
79 |
| -One approach is to serialize the data in some way—possibly by converting into a byte |
80 |
| -string. However, this usually ends up making a copy of the data. Even if you do this |
81 |
| -piecemeal, your code still ends up making a lot of little copies. |
| 78 | +在数据密集型分布式计算和平行计算程序中,自己写程序来实现发送/接受大量数据并不常见。 |
| 79 | +不过,要是你确实想这样做,你可能需要将你的数据转换成原始字节,以便给低层的网络函数使用。 |
| 80 | +你可能还需要将数据切割成多个块,因为大部分和网络相关的函数并不能一次性发送或接受超大数据块。 |
| 81 | + |
| 82 | +一种方法是使用某种机制序列化数据——可能将其转换成一个字节字符串。 |
| 83 | +不过,这样最终会创建数据的一个复制。 |
| 84 | +就算你只是零碎的做这些,你的代码最终还是会有大量的小型复制操作。 |
82 | 85 |
|
83 | 86 | This recipe gets around this by playing a sneaky trick with memoryviews. Essentially, a
|
84 | 87 | memoryview is an overlay of an existing array. Not only that, memoryviews can be cast
|
85 | 88 | to different types to allow interpretation of the data in a different manner. This is the
|
86 | 89 | purpose of the following statement:
|
87 |
| -view = memoryview(arr).cast('B') |
88 |
| - |
89 |
| -It takes an array arr and casts into a memoryview of unsigned bytes. |
90 |
| -In this form, the view can be passed to socket-related functions, such as sock.send() |
91 |
| -or send.recv_into(). Under the covers, those methods are able to work directly with |
92 |
| -the memory region. For example, sock.send() sends data directly from memory |
93 |
| -without a copy. send.recv_into() uses the memoryview as the input buffer for the |
94 |
| -receive operation. |
95 |
| -The remaining complication is the fact that the socket functions may only work with |
96 |
| -partial data. In general, it will take many different send() and recv_into() calls to |
97 |
| -transmit the entire array. Not to worry. After each operation, the view is sliced by the |
98 |
| -number of sent or received bytes to produce a new view. The new view is also a memory |
99 |
| -overlay. Thus, no copies are made. |
100 |
| -One issue here is that the receiver has to know in advance how much data will be sent |
101 |
| -so that it can either preallocate an array or verify that it can receive the data into an |
102 |
| -existing array. If this is a problem, the sender could always arrange to send the size first, |
103 |
| -followed by the array data. |
| 90 | +本节通过使用内存视图展示了一些魔法操作。 |
| 91 | +本质上,一个内存视图就是一个已存在数组的覆盖层。不仅仅是那样, |
| 92 | +内存视图还能以不同的方式转换成不同类型来表现数据。 |
| 93 | +这个就是下面这个语句的目的: |
| 94 | + |
| 95 | +.. code-block:: python |
| 96 | +
|
| 97 | + view = memoryview(arr).cast('B') |
| 98 | +
|
| 99 | +它接受一个数组 arr并将其转换为一个无符号字节的内存视图。这个视图能被传递给socket相关函数, |
| 100 | +比如 ``socket.send()`` 或 ``send.recv_into()`` 。 |
| 101 | +在内部,这些方法能够直接操作这个内存区域。例如,``sock.send()`` 直接从内存中发生数据而不需要复制。 |
| 102 | +``send.recv_into()`` 使用这个内存区域作为接受操作的输入缓冲区。 |
| 103 | + |
| 104 | +剩下的一个难点就是socket函数可能只操作部分数据。 |
| 105 | +通常来讲,我们得使用很多不同的 ``send()`` 和 ``recv_into()`` 来传输整个数组。 |
| 106 | +不用担心,每次操作后,视图会通过发送或接受字节数量被切割成新的视图。 |
| 107 | +新的视图同样也是内存覆盖层。因此,还是没有任何的复制操作。 |
104 | 108 |
|
| 109 | +这里有个问题就是接受者必须事先知道有多少数据要被发送, |
| 110 | +以便它能预分配一个数组或者确保它能将接受的数据放入一个已经存在的数组中。 |
| 111 | +如果没办法知道的话,发送者就得先将数据大小发送过来,然后再发送实际的数组数据。 |
105 | 112 |
|
0 commit comments