python-3.x 如何使用多处理值来传递图像base64字符串?

ehxuflar  于 2023-03-09  发布在  Python
关注(0)|答案(1)|浏览(106)

在我的一个项目中,我尝试在单独的multiprocessing.Process上运行Figure.savefig()(由于内存泄漏),但是我没有设法分配足够长的共享变量来传递图像字符串。
参见概念示例:

import base64
from multiprocessing import Process, Value

def my_func(shared_string):
    with open("image.png", "rb") as image_file:
       
        encoded_string = base64.b64encode(image_file.read())
        shared_string.value = encoded_string.decode()

if __name__ == "__main__":
    shared_string = Value('c', b'\0'*1024*1024) # Allocate 1MB of memory for the string
    print("Before:", shared_string.value)

    p = Process(target=my_func, args=(shared_string,))
    p.start()
    p.join()

    print("After:", shared_string.value)

但是我收到了错误,可能是因为我不知道如何正确地设置参数

File "C:\Users\MyName\MyFiles\test.py", line 10, in <module>
    shared_string = Value('c', b'\0'*1024*1024) # Allocate 1MB of memory for the string
  File "C:\Users\Name\miniconda3\lib\multiprocessing\context.py", line 135, in Value
    return Value(typecode_or_type, *args, lock=lock,
  File "C:\Users\MyName\miniconda3\lib\multiprocessing\sharedctypes.py", line 74, in Value
    obj = RawValue(typecode_or_type, *args)
  File "C:\Users\MyName\miniconda3\lib\multiprocessing\sharedctypes.py", line 51, in RawValue
    obj.__init__(*args)
TypeError: one character bytes, bytearray or integer expected

我也尝试使用multiprocessing.array来代替,但是图像字符串的大小超过了限制。

ghg1uchk

ghg1uchk1#

  1. a Value用于存储单个数字,您要查找的是Array
    1.对于数组,您需要使用array.raw属性而不是array.value
    1.只有bytes数组可以存储在固定大小的数组中,unicode字符串不能存储在bytes数组中,因为它没有固定大小(一个unicode字符可以占用多个字节),decode()bytesstr之间转换,这可能不是你想做的,如果你真的必须发送一个unicode字符串,你最好使用multiprocessing.Queue
    1.请注意,如果您通过http发送,您只需要b64encode
    下面对你的代码的修改显示了它是如何实现的。
import base64
from multiprocessing import Process, Value, Array

def my_func(shared_string):
    with open("my_file", "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
        shared_string.raw = encoded_string

if __name__ == "__main__":

    # write data to file, to make sure it is there
    with open("my_file",'wb') as f:
        orig_string = b"1"*8
        encoded_string = base64.b64decode(orig_string)
        f.write(encoded_string)

    shared_string = Array('c', b'\0' * len(orig_string))  # Allocate 8 bytes of memory for the string
    print("Before:", shared_string.raw)

    p = Process(target=my_func, args=(shared_string,))
    p.start()
    p.join()

    print("After:", shared_string.raw)

相关问题