我已经看过几篇关于使用Python Multiprocessing模块的内存使用情况的帖子.然而问题似乎没有回答我在这里遇到的问题.我发布我的分析,希望有人可以帮助我.
我正在使用多处理来并行执行任务,我注意到工作进程的内存消耗无限增长.我有一个小的独立示例,应该复制我注意到的.
import multiprocessing as mp
import time
def calculate(num):
l = [num*num for num in range(num)]
s = sum(l)
del l # delete lists as an option
return s
if __name__ == "__main__":
pool = mp.Pool(processes=2)
time.sleep(5)
print "launching calculation"
num_tasks = 1000
tasks = [pool.apply_async(calculate,(i,)) for i in range(num_tasks)]
for f in tasks:
print f.get(5)
print "calculation finished"
time.sleep(10)
print "closing pool"
pool.close()
print "closed pool"
print "joining pool"
pool.join()
print "joined pool"
time.sleep(5)
Run Code Online (Sandbox Code Playgroud)
我正在运行Windows,我使用任务管理器来监视内存使用情况.我正在运行Python …
我有一个项目(创建一个dll),例如test.dll,我没有在其中将类(例如TestClass)的构造函数和析构函数导出到dll中。这是因为我有一些工厂函数应该被调用来创建和销毁TestClass对象。在我创建尝试使用来自test.dll的TestClass对象的独立示例中,该设计效果很好。
但是,当我将此test.dll(或者由于我使用Visual Studio而在我的情况下为.lib)链接到生产模块中的项目时,我得到了奇怪的链接错误,指向构造函数和析构函数,它们在TestClass中找不到。我知道我不会在项目中的任何地方调用new / delete或创建TestClass的任何堆栈实例。我认为生产模块使用C#/ CLR / CLI以及C ++。唯一的解决方法是为我导出TestClass的构造函数和析构函数。这在设计上是不希望的。
任何人都熟悉这种情况吗?有人可以指出问题所在吗?
这是我得到的错误:
Error 5264 error LNK2028: unresolved token (0A000BA3) "public: virtual __thiscall BE::TestClass::~TestClass(void)" (??1TestClass@BE@@$$FUAE@XZ) referenced in function "public: virtual void * __thiscall BE::TestClass::`vector deleting destructor'(unsigned int)" (??_ETestClass@BE@@$$FUAEPAXI@Z) AMBestDetailBridge.obj BEBase
Error 5373 error LNK2001: unresolved external symbol "public: virtual __thiscall BE::TestClass::~TestClass(void)" (??1TestClass@BE@@$$FUAE@XZ) AMBestDetailBridge.obj BEBase
Run Code Online (Sandbox Code Playgroud)
谢谢!
我想对共享变量进行两次操作。我需要保证它可以原子地完成。有人可以帮我澄清以下方法是否正确:
#include <atomic>
std::atomic<int> index;
void function()
{
// I need the variable index to be incremented but bound in the
// range of [0,9].
int loc_indx = index.load(std::memory_order_acquire);
index.store( (loc_indx+1)%10 , std::memory_order_release);
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,索引存储操作和索引加载操作必须一起发生。这里的一些专家可以澄清上面的代码是否等同于以下伪代码:
ATOMIC
{
index = (index+1)%10;
}
Run Code Online (Sandbox Code Playgroud)
我一直在 Visual Studio 2012 的 c++ 部分或/和 1.53 的 boost::atomic 部分中使用原子包。
我在pandas DataFrame创建基准测试时发现它比numpy ndarray创建更昂贵.
基准代码
from timeit import Timer
setup = """
import numpy as np
import pandas as pd
"""
numpy_code = """
data = np.zeros(shape=(360,),dtype=[('A', 'f4'),('B', 'f4'),('C', 'f4')])
"""
pandas_code ="""
df =pd.DataFrame(np.zeros(shape=(360,),dtype=[('A', 'f4'),('B', 'f4'),('C', 'f4')]))
"""
print "Numpy",min(Timer(numpy_code,setup=setup).repeat(10,10))*10**6,"micro-seconds"
print "Pandas",min(Timer(pandas_code,setup=setup).repeat(10,10))*10**6,"micro-seconds"
Run Code Online (Sandbox Code Playgroud)
输出是
Numpy 17.5073728315 micro-seconds
Pandas 1757.9817013 micro-seconds
Run Code Online (Sandbox Code Playgroud)
我想知道是否有人可以帮助我理解为什么pandas DataFrame创作比ndarray建筑更昂贵.如果我做错了什么,请你帮我提高性能.
系统细节
pandas version: 0.12.0
numpy version: 1.9.0
Python 2.7.6 (32-bit) running on Windows 7
Run Code Online (Sandbox Code Playgroud) c++ ×2
python ×2
boost ×1
boost-thread ×1
c# ×1
clr ×1
concurrency ×1
memory ×1
numpy ×1
pandas ×1
performance ×1
visual-c++ ×1
windows ×1