python - 将 Python 中的字节从 Numpy 数组复制到字符串或字节数组中

标签 python arrays numpy buffer

我在 while 循环中从 UDP 套接字读取数据。我需要最有效的方法

1)读取数据(*)(这有点解决了,但感谢评论)

2)定期将(操纵的)数据转储到文件中(**)(问题)

我预计 numpy 的“tostring”方法会出现瓶颈。让我们考虑以下一段(不完整的)代码:

import socket
import numpy

nbuf=4096
buf=numpy.zeros(nbuf,dtype=numpy.uint8) # i.e., an array of bytes
f=open('dump.data','w')

datasocket=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# ETC.. (code missing here) .. the datasocket is, of course, non-blocking

while True:
  gotsome=True
  try:
    N=datasocket.recv_into(buf) # no memory-allocation here .. (*)
  except(socket.error):
    # do nothing ..
    gotsome=False

  if (gotsome):
    # the bytes in "buf" will be manipulated in various ways ..
    # the following write is done frequently (not necessarily in each pass of the while loop):
    f.write(buf[:N].tostring())  # (**) The question: what is the most efficient way to do this?

f.close() 

现在,在(**),据我所知:

1) buf[:N] 为一个长度为 N+1 的新数组对象分配内存,对吧? (也许不会)

.. 在那之后:

2) buf[:N].tostring() 为一个新的字符串分配内存,来自buf的字节被复制到这个字符串中

这似乎有很多内存分配和交换。在同一个循环中,将来,我将读取几个套接字并写入几个文件。

有没有办法告诉 f.write 直接访问“buf”的内存地址从 0 到 N 字节并将它们写入磁盘?

即,本着缓冲区接口(interface)的精神这样做并避免这两个额外的内存分配?

PS f.write(buf[:N].tostring()) 等价于 buf[:N].tofile(f)

最佳答案

基本上,听起来你想使用数组的 tofile方法或直接使用ndarray.data缓冲对象。

对于您的确切用例,使用数组的 data缓冲区是最有效的,但是对于一般用途,您需要注意很多警告。我稍后会详细说明。

但是,首先让我回答您的几个问题并提供一些说明:

buf[:N] allocates memory for a new array object, having the length N+1, right?



这取决于您所说的“新数组对象”是什么意思。无论涉及的数组大小如何,分配的额外内存都很少。

它确实为一个新的数组对象分配内存(几个字节),但它没有为数组的数据分配额外的内存。相反,它会创建一个共享原始数组数据缓冲区的“ View ”。您对 y = buf[:N] 所做的任何更改会影响buf也是。

buf[:N].tostring() allocates memory for a new string, and the bytes from buf are copied into this string



对,那是正确的。

附带说明一下,您实际上可以采用相反的方式(字符串到数组)而不分配任何额外的内存:
somestring = 'This could be a big string'
arr = np.frombuffer(buffer(somestring), dtype=np.uint8)

但是,由于 python 字符串是不可变的,arr将是只读的。

Is there a way to just tell f.write to access directly the memory address of "buf" from 0 to N bytes and write them onto the disk?



是的!

基本上,你会想要:
f.write(buf[:N].data)

这非常有效,适用于任何类似文件的对象。在这种情况下,这几乎肯定是您想要的。但是,有几个警告!

首先,请注意 N将在数组中的项目中,而不是直接以字节为单位。它们在您的示例代码中是等效的(由于 dtype=np.int8 或任何其他 8 位数据类型)。

如果你确实想写一些字节,你可以这样做
f.write(buf.data[:N])

...但是切片 arr.data buffer 将分配一个新的字符串,所以它在功能上类似于 buf[:N].tostring() .无论如何,请注意 f.write(buf[:N].tostring())不同于做 f.write(buf.data[:N])对于大多数 dtypes,但两者都会分配一个新字符串。

接下来,numpy 数组可以共享数据缓冲区。在您的示例情况下,您无需担心这一点,但通常使用 somearr.data由于这个原因,可能会导致惊喜。

举个例子:
x = np.arange(10, dtype=np.uint8)
y = x[::2]

现在,yx 共享相同的内存缓冲区,但它在内存中并不连续(查看 x.flagsy.flags )。相反,它引用 x 中的所有其他项目的内存缓冲区(比较 x.stridesy.strides )。

如果我们尝试访问 y.data ,我们会得到一个错误,告诉我们这不是内存中的连续数组,我们无法为它获取单段缓冲区:
In [5]: y.data
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-54-364eeabf8187> in <module>()
----> 1 y.data

AttributeError: cannot get single-segment buffer for discontiguous array

这是 numpy 数组具有 tofile 的很大一部分原因。方法(它也早于 python 的 buffer s,但这是另一回事)。
tofile将数组中的数据写入文件而不分配额外的内存。但是,因为它是在 C 级实现的,所以它只适用于真正的 file对象,而不是类似文件的对象(例如套接字、StringIO 等)。

例如:
buf[:N].tofile(f)

但是,这是在 C 级别实现的,并且仅适用于实际的文件对象,而不适用于套接字、StringIO 和其他类似文件的对象。

但是,这确实允许您使用任意数组索引。
buf[someslice].tofile(f)

将创建一个新 View (相同的内存缓冲区),并有效地将其写入磁盘。在您的确切情况下,它会比切片 arr.data 稍慢缓冲区并直接将其写入磁盘。
如果您更喜欢使用数组索引(而不是字节数),那么 ndarray.tofile方法将比 f.write(arr.tostring()) 更有效.

关于python - 将 Python 中的字节从 Numpy 数组复制到字符串或字节数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28341785/

相关文章:

Python:计算 numpy 数组(大数据集)中出现次数的更快方法

python - pip 安装的 uWSGI ./python_plugin.so 错误

javascript - 在javascript中从json数组动态生成表

c - 使用 C 返回数组

python - 从 numpy 数组中采样固定长度的序列

python - 通过 FontForge Python 绑定(bind)更改字体的 em 大小

python - 如何在列的两个给定值之间选择数据框?

python - 在 numpy 一维数组中查找拐点和固定点

python - Numpy 数组文档切片规则

python-2.7 - Virtualenv 导入 Numpy 时遇到问题