0

I'm trying to call c++ program from python, that connects to mongo using mongocxx , queries some data and returns the resultset to python. Here's my c++ program. contents of c++ program file

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <iostream>

extern "C" std::vector<std::string> data(){

std::vector<std::string> arr;

mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};

bsoncxx::builder::stream::document document{};

auto collection = conn["watchman"]["testcol"];
auto cursor = collection.find({});

for (auto&& doc : cursor) {
   std::cout << bsoncxx::to_json(doc) << std::endl;
   arr.push_back(bsoncxx::to_json(doc));
}


return arr;

}

To compile the C++ file i am using:

g++ --std=c++11 -c -fPIC program.cpp -o program.o $(pkg-config --cflags --libs libmongocxx) -Wl,-rpath,/usr/local/lib

g++ -shared -Wl,-soname,library.so -o library.so program.o

contents of python program that calls c++ program.

import ctypes
from numpy.ctypeslib import ndpointer

lib = ctypes.CDLL('./library.so')
lib.function.restype = ndpointer(dtype=ctypes.c_int, shape=(10,))

res = lib.data()
print res

error that i get

Traceback (most recent call last):
  File "d.py", line 4, in <module>
    lib = ctypes.CDLL('./library.so')
  File "/usr/lib64/python2.6/ctypes/__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ./library.so: undefined symbol: _ZN8mongocxx7v_noabi3uri13k_default_uriE

Any help would be appreciated.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Why? There already is a python driver, and guess which language it uses for the low level bindings. Do you actually have some sound reason for attempting to use the c++ code and not the python implementation? Or did you just "presume" it would be faster? – Neil Lunn May 07 '18 at 10:43
  • Also FYI, the Python driver is usually the driver which gets all the latest updates "first". So it's actually the most regularly maintained of all the officially supported drivers. – Neil Lunn May 07 '18 at 10:46
  • django/python/tastypie takes around >5mins to return back 60K rows from mongo. so i was exploring mongocxx driver mainly for the purpose of speed – anubhav231989 May 07 '18 at 10:57
  • Did you possibly consider it's not the "python" part that takes so long, but possibly the way you were doing it? Clearly C++ isn't your bag so rather than "presuming" that was the way to improve things, you really should have approached us here asking how to improve the python code instead. Because C++ alone is not going to give you the performance boost you are looking for anyway. – Neil Lunn May 07 '18 at 11:01
  • i did few rounds of optimizations to the tastypie resource, that brought the speed to half. also, started using tastypie resource on raw pymongo objects. when i couldn't make the resource go any faster, i thought of implementing the resource on low level lang. – anubhav231989 May 07 '18 at 11:06

0 Answers0