Python共享内存,如何将随机整数放入共享内存块中?
我创建了一个字节大小为10的内存块,并想创建一个随机数并将其放入内存块中,但它总是会给我错误消息,所以我想知道我是否做错了。
from multiprocessing import shared_memory
import random
shared_mem_1 = shared_memory.SharedMemory(create=True, size=10)
num = (random.sample(range(1, 1000), 10))
for i, c in enumerate(num):
shared_mem_1.buf[i] = c
错误消息:
Traceback (most recent call last):
File "main.py", line 7, in <module> shared_mem_1.buf[i] = c
ValueError: memoryview: invalid value for format 'B'
I created a memory block with a Byte size of 10 and wanted to create a random number and put it into the Memory block but it always just gives me error messages so I wonder if I am doing it wrong.
from multiprocessing import shared_memory
import random
shared_mem_1 = shared_memory.SharedMemory(create=True, size=10)
num = (random.sample(range(1, 1000), 10))
for i, c in enumerate(num):
shared_mem_1.buf[i] = c
The error-message:
Traceback (most recent call last):
File "main.py", line 7, in <module> shared_mem_1.buf[i] = c
ValueError: memoryview: invalid value for format 'B'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于
num
包含255上的值以及分配给buf
格式的无效值'b'b'
出现错误。格式
b
正是字节的格式(在此处检查格式表)。有2个选项:
int.int.to_bytes
功能。选项1
选项2
的选项2您需要注意字节订单(大端/小式)以及整数在您的情况下具有多少个字节(另外,分配的内存量都取决于此长度)。对缓冲区的分配应计算已保存的偏移量。
The problem is that
num
contains values over 255 and when it's assigned tobuf
the invalid value for format'B'
error appears. FormatB
is exactly the format for bytes (Check the table of formats here).There are 2 options:
int.to_bytes
function.Option 1
Option 2
For option 2 you'd need to pay attention to the bytes order (big-endian/little-endian) and how many bytes an integer has in your case (Also, the amount of memory to allocate depends on this length). The assignment to the buffer should calculate the offset it saved already.
我发现利用
多处理。Shared_memory
的最有用方法是创建一个使用共享内存区域作为其内存缓冲区的Numpy数组。 numpy处理设置正确的数据类型(它是8位整数?一个32位float?64 lit float?et。 模块)。这样,对数组的任何修改都可以在具有相同内存区域映射到数组的任何过程中可见。I find the most useful way to take advantage of
multiprocessing.shared_memory
is to create a numpy array that uses the shared memory region as it's memory buffer. Numpy handles setting the correct data type (is it an 8 bit integer? a 32 bit float? 64 bit float? etc..) as well as providing a convenient interface (similar, but more extensible than python's built-inarray
module). That way any modifications to the array are visible across any processes that have that same memory region mapped to an array.数十年来,我一直在与CSV文件的两个同时运行脚本之间共享值,没有任何问题。我试图切换直接分享。
这是我带有共享的测试代码。我在另一个线程中发布了共享_memory_dict的测试代码。共享_memory不能共享负值,而_ dict可以。源文件:srcarry2.py
接收文件:rcvarry2.py
buf [4]在接收文件中已更改。源文件必须在接收文件之前启动。当时不存在Buf [4],因此处理了一个例外。
I have been sharing values between two concurrently running scripts with csv files for decades without any problem. I was trying to switch to share directly.
Here are my test codes with shared_memory. I posted the test codes for shared_memory_dict in another thread. The shared_memory cannot share negative values whereas _dict can. Source file : SrcArry2.py
Receiving file: RcvArry2.py
The buf[4] is changed in receiving file. The source file has to be started before the receiving file. The buf[4] doesn't exist at that time so an exception was handled.