Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading a c++ program in python

I have a Python application written in Kivy that uses a C++ program for a high speed calculation, then it returns a value and my Python application uses that.

The C++ program is wrapped in PyBind11 and imported into the application and then called from Python.

My issue is when the C++ program is executed, my application stops for a short while and I'd still like things to be going on in the background.

I naively thought this could be solved by threading the C++ call, but on second thoughts I think the issue lies in the GIL. Must I unlock the GIL and how could I achieve this?

like image 513
ljelliot Avatar asked Nov 26 '25 02:11

ljelliot


1 Answers

Without seeing any code, I can only deduce that your Python code is waiting for the C++ code to complete before doing anything else. Which can mean either or both of the following:

  • you are not unlocking the GIL in the C++ code

    • According to Global Interpreter Lock (GIL) — Miscellaneous — pybind11 2.2.3 documentation, with pybind, this is supposed to be done like this:

      py::gil_scoped_release release;
      long_running_method();
      py::gil_scoped_acquire acquire;
      

      Note that you need the GIL to access any Python machinery (including returning the result). So before releasing it, make sure to convert all the data you need from Python types to C++ types.

  • you don't have any other active Python threads, so there's no other Python activity programmed in to do anything while the C++ call is in progress

like image 61
ivan_pozdeev Avatar answered Nov 27 '25 15:11

ivan_pozdeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!