Sto cercando di avvolgere un pezzo di codice C++ in pitone lib utilizzando Boost.Python, però, ho scoperto che più istanze non possono funzionare allo stesso tempo:boost.python non supporta il parallelismo?
codice (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)
;
}
che è compilare usando CMake (CMake):
add_library(run_test SHARED run_test.cpp)
target_link_libraries(run_test boost_python python2.7)
e testato con il seguente codice (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()
L'output indica che il codice è parallelizzato in un modo molto strano. Ogni thread dovrebbe richiedere solo 2 secondi e 4 thread dovrebbero essere eseguiti contemporaneamente sulla mia macchina quadcore:
[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
grazie per il vostro aiuto!
Bene , questo certamente sembra un'applicazione legittima ...;) – larsmoa
Questo sarebbe più facile da leggere se indicassi quale codice fosse python e quale fosse C++. L'ho capito, ma mi ci è voluto un momento. –