- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我启动多个进程以创建新对象列表。 htop
向我展示了 1 到 4 个进程(我总是创建 3 个新对象)。
def foo(self):
with multiprocessing.Pool(processes=3, maxtasksperchild=10) as pool:
result = pool.map_async(self.new_obj, self.information)
self.new_objs = result.get()
pool.terminate()
gc.collect()
foo()
多次,每次调用时,整个过程运行速度较慢,程序甚至没有完成,因为它减慢了很多。该程序开始占用我所有的 RAM,而顺序方法没有任何显着的 RAM 使用量。
->File "threading.py", line 293, in wait
waiter.acquire()
foo()
由父节点调用以创建其子节点。
result
进程返回的是这些子节点。这些保存在父节点的列表中。我想并行化这些子节点的创建,而不是按顺序创建它们。
最佳答案
我认为您的问题主要与您的并行函数是对象的方法这一事实有关。如果没有更多信息,很难确定,但请考虑这个小玩具程序:
import multiprocessing as mp
import numpy as np
import gc
class Object(object):
def __init__(self, _):
self.data = np.empty((100, 100, 100), dtype=np.float64)
class Container(object):
def __new__(cls):
self = object.__new__(cls)
print("Born")
return self
def __init__(self):
self.objects = []
def foo(self):
with mp.Pool(processes=3, maxtasksperchild=10) as pool:
result = pool.map_async(self.new_obj, range(50))
self.objects.extend(result.get())
pool.terminate()
gc.collect()
def new_obj(self, i):
return Object(i)
def __del__(self):
print("Dead")
if __name__ == '__main__':
c = Container()
for j in range(5):
c.foo()
Container
仅被调用一次,因此您希望看到
"Born"
,然后是
"Dead"
被打印出来;但是由于进程执行的代码是容器的方法,这意味着整个容器必须在其他地方执行!运行这个,你会看到一个混合流
"Born"
和
"Dead"
当您的容器在每次执行 map 时重建:
Born
Born
Born
Born
Born
Dead
Born
Dead
Dead
Born
Dead
Born
...
<MANY MORE LINES HERE>
...
Born
Dead
def foo(self):
with mp.Pool(processes=3, maxtasksperchild=10) as pool:
result = pool.map_async(self.new_obj, range(50))
self.fn = lambda x: x**2
self.objects.extend(result.get())
pool.terminate()
gc.collect()
AttributeError
因为它不能序列化容器。
Container
将被序列化,发送到进程并在那里反序列化 1000 次。当然,它们最终会被删除(假设没有太多奇怪的交叉引用发生),但这肯定会给 RAM 带来很大压力,因为对象被序列化、调用、更新、重新序列化......对于每个映射输入中的元素。
def new_obj(_):
return Object(_)
class Container(object):
def __new__(cls):
self = object.__new__(cls)
print("Born")
return self
def __init__(self):
self.objects = []
def foo(self):
with mp.Pool(processes=3, maxtasksperchild=10) as pool:
result = pool.map_async(new_obj, range(50))
self.objects.extend(result.get())
pool.terminate()
gc.collect()
def __del__(self):
print("Dead")
Container
)。如果您需要在那里传递一些内部状态,请将其提取并发送:
def new_obj(tup):
very_important_state, parameters = tup
return Object(very_important_state=very_important_state,
parameters=parameters)
class Container(object):
def __new__(cls):
self = object.__new__(cls)
print("Born")
return self
def __init__(self):
self.objects = []
def foo(self):
important_state = len(self.objects)
with mp.Pool(processes=3, maxtasksperchild=10) as pool:
result = pool.map_async(new_obj,
((important_state, i) for i in range(50)))
self.objects.extend(result.get())
pool.terminate()
gc.collect()
def __del__(self):
print("Dead")
关于python-3.x - Python3 : Multiprocessing consumes extensively much RAM and slows down,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38140693/
如果我们想访问数组的元素 i,我们会这样做:i *(每个元素的大小)+ 数组的起始地址 = 我们需要访问的元素的内存位置。然后 CPU 直接访问 RAM 中的该位置。这是怎么发生的? 假设我要定位的内
考虑以下 python 代码 with open(sys.argv[2], 'r') as fin, \ open(sys.argv[3], 'w') as fout:
基本上我在 Centos 6.2 16GB 系统上运行我的世界服务器 我通过 rtoolkit 引导来启动我的服务器,现在我分配它使用 12GB,但它使用的 muc 比这多,我不知道为什么。我运行此命
我正在开发一个小型 x86 内核。我正在访问并尝试读取 GRUB 在多重引导 header 中提供的内存映射。我有一个 Intel i3 cpu 和 4 GiB 的 RAM。在这台机器上运行时,我正在
我有 3 GB 内存。有没有办法只为 C++ 应用程序分配 512MB 的 RAM? 否则 有没有办法暂时将我的 RAM 减少到 512MB 以进行测试? 谢谢,阿肖克 最佳答案 使用SetProce
我的一个 friend 告诉我,在 x86 架构上,DMA Controller 无法在两个不同的 RAM 位置之间传输。它只能在RAM和外设(如PCI总线)之间传输。 这是真的吗? 因为 AFAIK
做一些分析(内存和速度) 我一直被 win7 似乎分配的 RAM 正好是我要求的两倍这一事实所困扰...注意这是我第一次在 win7 上进行这样的主动分析,所以我真的不知道会发生什么。 我在 win7
我经常玩 retrofit 版的 Minecraft。这样做的一个缺点是,每当我启动 Minecraft 时,编译所有模组都需要花费大量时间。这可能需要大约 15 分钟左右的时间,在我看来,这太长了。
我想编写一个内核模块,可以通过 DMA 将数据从 RAM 传输到 RAM。有一些帖子讨论这个,但我真的不明白。有人说可能,也有人说不可能。 如果我对 ldd3 的理解是正确的,RAM 到 RAM 的复
我正在寻找一种方法来查找总内存和正在使用的内存的单个熟值输出。 gwmi Win32_OperatingSystem | select TotalVisibleMemorySize, FreePhys
我在 Heroku 上有一个网络应用程序,我试图了解添加具有 1GB RAM 的 Memcached 实例与向我的 Postgres 服务器添加 1GB RAM 之间的区别/权衡。 如果我添加一个 M
在我的例子中,我有一个包含大约 6000 个实例化类的字典,其中每个类都有 1000 个属性变量,所有类型都是字符串或字符串列表。当我建立这本字典时,我的 RAM 变得非常高。有没有一种方法可以在将字
我的问题是:如果您用完了所有可用的视频内存,并尝试创建新纹理 (SDL),是否会自动使用普通内存而不是视频内存?或者,您是否必须尝试使用使用普通 ram 的表面 (SDL)?如果您出于任何原因无法
我在从 ram 地址获取值时遇到问题。 我有一些限制,即它必须是 C 代码,并且存储信息的地址由 u32 值给出,例如 0x001c0080。 环境是ARM的,我用的是eclipse sdk。我怎样才
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我正在尝试使用 GCC 编译器(标准 C)编译裸机应用程序。我使用 Cyclone V SoC 和 Cortex-A9 处理器。 eclipse DS-5。我收到这些错误 - “Region ram
如果 RAM 不是问题(我的服务器上有接近 200GB),是逐行读取更快还是将所有内容读入 RAM 并访问它?每行将是大约 200-500 个 unicode 字符的字符串。每个文件有近 200 万行
我在我的两台机器上运行以下命令: import os, sqlite3 import pandas as pd from feat_transform import filter_anevexp db
我正在尝试在我的一个项目中引入一些 CUDA 优化。但我认为我在这里做错了什么。我想实现一个简单的矩阵 vector 乘法 (result = matrix * vector)。但是当我想将结果复制回
#include #include #include #include #include #include using namespace std; char randomLetter()
我是一名优秀的程序员,十分优秀!