[Python] Python shared memory, how can I put the random integer into the shared memory block?

Discussão em 'Python' iniciado por Stack, Janeiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    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 = c


    The error-message:

    Traceback (most recent call last):
    File "main.py", line 7, in <module> shared_mem_1.buf = c
    ValueError: memoryview: invalid value for format 'B'

    Continue reading...

Compartilhe esta Página