Skip to content

Instantly share code, notes, and snippets.

@safijari
Created April 1, 2021 20:56
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save safijari/f7aec85b89906b4b90a8f33039c11263 to your computer and use it in GitHub Desktop.
Save safijari/f7aec85b89906b4b90a8f33039c11263 to your computer and use it in GitHub Desktop.
The code relevant to pybind11 video
cmake_minimum_required(VERSION 3.4...3.18)
project(pybindtest)
add_subdirectory(pybind11)
pybind11_add_module(module_name main.cpp)
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <chrono>
#include <thread>
namespace py = pybind11;
float some_fn(float arg1, float arg2) {
return arg1 + arg2;
}
class SomeClass {
float multiplier;
public:
SomeClass(float multiplier_) : multiplier(multiplier_) {};
float multiply(float input) {
return multiplier * input;
}
std::vector<float> multiply_list(std::vector<float> items) {
for (auto i = 0; i < items.size(); i++) {
items[i] = multiply(items.at(i));
}
return items;
}
// py::tuple multiply_two(float one, float two) {
// return py::make_tuple(multiply(one), multiply(two));
// }
std::vector<std::vector<uint8_t>> make_image() {
auto out = std::vector<std::vector<uint8_t>>();
for (auto i = 0; i < 128; i++) {
out.push_back(std::vector<uint8_t>(64));
}
for (auto i = 0; i < 30; i++) {
for (auto j = 0; j < 30; j++) { out[i][j] = 255; }
}
return out;
}
void set_mult(float val) {
multiplier = val;
}
float get_mult() {
return multiplier;
}
void function_that_takes_a_while() {
py::gil_scoped_release release;
std::cout << "starting" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::cout << "ended" << std::endl;
// py::gil_scoped_acquire acquire;
// auto list = py::list();
// list.append(1);
}
};
SomeClass some_class_factory(float multiplier) {
return SomeClass(multiplier);
}
PYBIND11_MODULE(module_name, module_handle) {
module_handle.doc() = "I'm a docstring hehe";
module_handle.def("some_fn_python_name", &some_fn);
module_handle.def("some_class_factory", &some_class_factory);
py::class_<SomeClass>(
module_handle, "PySomeClass"
).def(py::init<float>())
.def_property("multiplier", &SomeClass::get_mult, &SomeClass::set_mult)
.def("multiply", &SomeClass::multiply)
.def("multiply_list", &SomeClass::multiply_list)
// .def_property_readonly("image", &SomeClass::make_image)
.def_property_readonly("image", [](SomeClass &self) {
py::array out = py::cast(self.make_image());
return out;
})
// .def("multiply_two", &SomeClass::multiply_two)
.def("multiply_two", [](SomeClass &self, float one, float two) {
return py::make_tuple(self.multiply(one), self.multiply(two));
})
.def("function_that_takes_a_while", &SomeClass::function_that_takes_a_while)
;
}
import time
import traceback
import cv2
from build.module_name import *
from concurrent.futures import ThreadPoolExecutor
def call_and_print_exc(fn):
try:
fn()
except Exception:
traceback.print_exc()
print(PySomeClass)
m = some_class_factory(10)
m2 = PySomeClass(10)
print(m, m2)
print(m.multiply(20))
# print(m.multiply("20"))
arr = m.multiply_list([0.0, 1.0, 2.0, 3.0])
print(arr)
print(m.multiply_two(50, 200))
print(m.image)
print(m.image.shape)
cv2.imwrite("/tmp/test.png", m.image)
print(m.multiplier)
m.multiplier = 100
print(m.multiplier)
start = time.time()
with ThreadPoolExecutor(4) as ex:
ex.map(lambda x: m.function_that_takes_a_while(), [None]*4)
print(f"Threaded fun took {time.time() - start} seconds")
@safijari
Copy link
Author

safijari commented Apr 3, 2021

Make sure the relevant python-dev packages are installed!

@mojtabaebrahimi2001
Copy link

this is very nice!
thank you

@geonuklee
Copy link

Thank you so much! It is very helpful for me.

@Joseda8
Copy link

Joseda8 commented Feb 18, 2022

Thank you so much!

@KC-git-usr
Copy link

KC-git-usr commented Sep 12, 2022

Hi, thanks a bunch for making this tutorial. I'm stuck on something and would appreciate your input:

  1. What editor are you using? Did you have to configure something to enable intelli-sense?
  2. I'm not able to get intellisense working for the imported .so module in pycharm, any tips?

@epmpub
Copy link

epmpub commented Nov 14, 2022

Thank you so much!

@xuanvuzzz2601
Copy link

Hi, thanks a bunch for making this tutorial. I'm stuck on something and would appreciate your input:

  1. What editor are you using? Did you have to configure something to enable intelli-sense?
  2. I'm not able to get intellisense working for the imported .so module in pycharm, any tips?

me too

@User-DK
Copy link

User-DK commented Mar 16, 2024

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment