【发布时间】:2011-11-04 13:06:53
【问题描述】:
我正在尝试使用 boost.python 将一段 C++ 代码包装到 python lib 中,但是,我发现多个实例不能同时运行:
代码(C++):
class Foo{
public:
Foo(){}
void run(){
int seconds = 2;
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
};
BOOST_PYTHON_MODULE(run_test)
{
using namespace boost::python;
class_<Foo>("test", init<>())
.def("run", &Foo::run)
;
}
这是使用CMake(CMake)编译的:
add_library(run_test SHARED run_test.cpp)
target_link_libraries(run_test boost_python python2.7)
并使用以下代码 (Python) 进行测试:
class Dos(threading.Thread):
def run(self):
printl('performing DoS attack')
proc = test()
proc.run()
for i in range(5):
t = Dos()
t.start()
输出表明代码以一种非常奇怪的方式并行化。每个线程应该只需要 2 秒,并且应该在我的四核机器上同时运行 4 个线程:
[2011-11-04 13:57:01] performing DoS attack
[2011-11-04 13:57:01] performing DoS attack
[2011-11-04 13:57:05] performing DoS attack
[2011-11-04 13:57:05] performing DoS attack
[2011-11-04 13:57:09] performing DoS attack
感谢您的帮助!
【问题讨论】:
-
嗯,这看起来确实像一个合法的应用程序... ;)
-
如果您指出哪些代码是python,哪些是c++,这将更容易阅读。我想通了,但我花了一点时间。
标签: c++ boost parallel-processing boost-python