安装python
安装boost
(1) Download boost_1_60_0.tar.bz2.
(2) 解压
tar –bzip2 -xf /path/to/boost_1_60_0.tar.bz2
(3)
./bootstrap.sh –with-libraries=python –prefix=/work/na/install/boost –with-python-root=/work/na/install/python
(4) ./b2 install
环境
os: linux
software: eclipse
eclipse
include: /work/na/install/boost/include
/work/na/install/python/include/python2.7
library search path:
/work/na/install/boost/lib/
/work/na/install/python/lib/
/work/na/install/python/lib/python2.7
libraries
boost_system
boost_python
python2.7
newtest.py
from threading import Lock, Thread
lock = Lock()
#thread = None
train_frames = 16
train_buffer = 24
global ix
ix=0
trainbatch="test"
class BatchAdvancer():
def __call__(self):
global ix
ix=ix+1
print ix
if cmp(trainbatch,"test"):
print trainbatch
class videoRead():
def __init__(self):
self.batch_advancer = BatchAdvancer()
self.thread=None
lock.acquire()
########################################
self.dispatch_worker()
########################################
########################################
self.join_worker()
########################################
lock.release()
def dispatch_worker(self):
assert self.thread is None
self.thread = Thread(target=self.batch_advancer)
self.thread.start()
def join_worker(self):
assert self.thread is not None
self.thread.join()
self.thread = None
'''
if __name__=='__main__':
a=videoRead()
b=videoRead()
'''
newtest2.py
这个文件放在/work/na下
global ix
ix=-5
def advance_batch(result):
global ix
ix=ix+1
result[0]=ix
class BatchAdvancer():
def __init__(self, result):
self.result = result
def __call__(self):
return advance_batch(self.result)
class videoRead():
def setup(self): #__init__(self):
self.result=[0,]
self.batch_advancer = BatchAdvancer(self.result)
self.batch_advancer()
return self.result
'''
if __name__=="__main__":
a=videoRead()
print a.setup()
b=videoRead()
print b.setup()
'''
main
#if 1
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/python/call_method.hpp>
#include <vector>
#include <iostream>
#include <Python.h>
#include <string>
#include <boost/thread/thread.hpp>
boost::mutex io_mutex;
#include <boost/python/stl_iterator.hpp>
using namespace std;
namespace bp = boost::python;
int py=1;
template<typename T>
void python_to_vector(bp::object o, vector<T> &v) {
bp::stl_input_iterator<T> begin(o);
bp::stl_input_iterator<T> end;
v.clear();
v.insert(v.end(), begin, end);
}
void newPython(){
boost::mutex::scoped_lock
lock(io_mutex);
Py_Initialize();
try
{
bp::object module;
bp::object instance;
switch(py){
case 1:
{
module = bp::import("newtest");
instance = module.attr("videoRead")();
break;
}
case 2:
{
module = bp::import("newtest2");
instance = module.attr("videoRead")();
bp::object top = instance.attr("setup")();
vector<int> res;
python_to_vector(top, res);
for(int i=0;i<res.size();i++)
cout<<res[i]<<endl;
break;
}
}
}
catch(...)
{
PyErr_Print();
PyErr_Clear();
}
}
int main()
{
Py_Initialize();
try
{
bp::object module = bp::import("sys");
bp::object pythonpath = module.attr("path");
bp::object temp = pythonpath.attr("append")("/work/na");
/*pythonpath = module.attr("path");
vector<string> paths;
python_to_vector(pythonpath, paths);
for(int i=0;i<paths.size();i++)
cout<<paths[i]<<endl;*/
}
catch(...)
{
PyErr_Print();
PyErr_Clear();
}
switch(py){
case 2:
{
newPython();
newPython();
break;
}
case 1:
{
boost::thread thrd1(&newPython);
boost::thread thrd2(&newPython);
thrd1.join();
thrd2.join();
break;
}
}
return 0;
}
#endif