将Python对象放置在共享内存中 [英] Placing Python objects in shared memory

查看:425
本文介绍了将Python对象放置在共享内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个Python模块可以使我将非平凡用户类的实例放置到共享内存中?

Is there a Python module that would enable me to place instances of non-trivial user classes into shared memory?

我的意思是直接在共享内存中分配为

By that I mean allocating directly in shared memory as opposed to pickling into and out of it.

multiprocessing.Value multiprocessing.Array 不适用于我的用例,因为它们似乎仅支持原始类型及其数组。

multiprocessing.Value and multiprocessing.Array wouldn't work for my use case as they only seem to support primitive types and arrays thereof.

到目前为止,我唯一发现的是 POSH ,但八年来没有改变。这表明它要么非常稳定,要么已经过时。在我花时间尝试使其工作之前,我想知道是否还有其他我尚未发现的替代方法。

The only thing I've found so far is POSH, but it hasn't changed in eight years. This suggests that it's either super-stable or is out of date. Before I invest time in trying to get it work, I'd like to know if there are alternatives I haven't discovered yet.

我只需要这样做即可Linux。

I only need this to work on Linux.

推荐答案

这有点困难。我能想到的最佳解决方案是腌制对象并使用 c_char_p multiprocessing.sharedctypes 。您仍然必须通过转义或转换为十六进制来确保没有空字节进入c_char_p。

That's kind of a tough one. The best solution I can think of is pickling your objects and using a c_char_p with multiprocessing.sharedctypes. You'd still have to make sure no null bytes got into the c_char_p, either by escaping them or converting to hex.

再三考虑,也许您应该使用POSH

On second thought, maybe you should go with POSH.

这篇关于将Python对象放置在共享内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆