简体   繁体   English

如何在多进程中共享大对象?

[英]how to share large object in multiprocess?

I want to do large calculation in python. 我想在python中进行大型计算。 and I want to use multiprocess to do it. 我想使用多进程来做到这一点。 The problem is that I must transport large obj(such as large numpy and list,or list of numpy), from main process to the process created by main process 问题是我必须从主流程到由主流程创建的流程中传输大型obj(例如大numpy和list或numpy列表)

Here is the problem: 1. if I use like results[i] = pool.apply_async(func, args_tuple) then it will try to pickle the args_tuple ,which cost large time 2. my own class and numpy class is not supported 3. all the object has been prepared in the main process, if I get it in the generated process ,it will cost time also. 这是问题所在:1.如果我使用类似results[i] = pool.apply_async(func, args_tuple) ,则会尝试腌制args_tuple,这会花费大量时间2.不支持我自己的类和numpy类3。所有的对象都已经在主流程中准备好了,如果我在生成的流程中得到它,那也会花费时间。

How could I solve this problem.. Maybe I should use java if I had predicted this, I looked it up in the doc, but it seems that every solution is the same -- pickle the params from main process to others (cost time) 我该如何解决这个问题。如果我曾预料到这一点,也许应该使用Java,我在文档中对其进行了查找,但似乎每个解决方案都是相同的-从主流程到其他流程的参数腌制(花费时间)

As far as I know , every way of communicating between process use pickle to transfer the data ( Pipes , Queue and others...) 据我所知 ,进程之间的每种通信方式都使用pickle来传输数据( PipesQueue和其他...)

One way to make your issue work could be to work on shared variables, but you can't work on the same object with multiples process at the same time so it depends on your application. 解决问题的一种方法是使用共享变量,但是您不能同时使用多个过程处理同一个对象,因此它取决于您的应用程序。 Their is some simple implemented shared variables in multiprocessing : Array and Value which are respectively a 1D array and a single value (int, float, str...). 它们是在多处理中一些简单实现的共享变量: ArrayValue ,它们分别是一维数组和单个值(int,float,str ...)。 See the documentation here. 请参阅此处的文档。

But you can also create your own types if you need to, using ctype : see the documentation here. 但是您也可以根据需要使用ctype创建自己的类型: 请参见此处的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM