Created
April 1, 2021 20:56
-
-
Save safijari/f7aec85b89906b4b90a8f33039c11263 to your computer and use it in GitHub Desktop.
The code relevant to pybind11 video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.4...3.18) | |
project(pybindtest) | |
add_subdirectory(pybind11) | |
pybind11_add_module(module_name main.cpp) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
this is very nice!
thank you
Thank you so much! It is very helpful for me.
Thank you so much!
Hi, thanks a bunch for making this tutorial. I'm stuck on something and would appreciate your input:
- What editor are you using? Did you have to configure something to enable intelli-sense?
- I'm not able to get intellisense working for the imported .so module in pycharm, any tips?
Thank you so much!
Hi, thanks a bunch for making this tutorial. I'm stuck on something and would appreciate your input:
- What editor are you using? Did you have to configure something to enable intelli-sense?
- I'm not able to get intellisense working for the imported .so module in pycharm, any tips?
me too
Thanks!!
I had a migraine over trying to use ctypes. You're video / code was a huge boon to my task!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure the relevant python-dev packages are installed!