pybind / pybind/pybind11

C++ generates data in real time, how to receive data in real time through pybind11 in python?

Open
#4,740 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

2.11.0dev1

Problem description

I am now using the deep learning framework to generate some target object detection box boxes and other information, and then I want to use pybind11 to encapsulate this information for my python function to use. It should be noted that the information of these box boxes is updated and changed in real time Yes, how should I get the data passed through pybind11 in the python function, and these data also change together. At present, the way I try to get all the empty values, I am a novice in C++, all now It's hard to keep going, hope you can help, thanks

my envs:
ubuntu18.04
python3.8.10
gcc 7.5.0

Reproducible example code
my codes:
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/stl.h>
#include <nlohmann/json.hpp>
#include <mutex>

namespace py = pybind11;

void initializeInterpreter() {
    py::initialize_interpreter();
}

static std::once_flag initFlag;
static py::dict py_json;

py::dict get_data() {
    return py_json;
}

void convert_json_cxx_2_py(const nlohmann::json& json_data) {
    std::call_once(initFlag, initializeInterpreter);
    py::gil_scoped_acquire acquire;
    py_json = py::module::import("json").attr("loads")(json_data.dump());
    get_data();
    py::gil_scoped_release release;
}

PYBIND11_MODULE(cxx_json_2_py, m) {
    m.def("convert_json_cxx_2_py", &convert_json_cxx_2_py, "Convert nlohmann::json to Python JSON");
    m.def("get_data", &get_data, "Get data converted from nlohmann::json to Python JSON", py::return_value_policy::reference);
    m.attr("py_json") = py::cast(&py_json);
}

function call location:
nlohmann::json jsondata = helmet_alarm(img, res, rtsp, 10, "no-helmet");

if (!jsondata.empty()) {
    convert_json_cxx_2_py(jsondata);
}
And the function input parameters nlohmann::json jsondat here are changed in real time

the python scipt:
import cxx_json_2_py

r = cxx_json_2_py.py_json
# r = cxx_json_2_py.get_data() or
print('res', r)

the python function return:
res {}
core dumped
Is this a regression? Put the last known working version here if it is.

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproducer and reviewing initializeInterpreter, convert_json_cxx_2_py, get_data, and the PYBIND11_MODULE bindings. Compare their behavior with the pybind11 documentation, especially around interpreter and GIL handling; done should be a minimal reproducer with clear expected real-time data behavior and no empty result or core dump.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.