c++调用Python [boost::python]

15 篇文章 0 订阅

安装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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值