Mutli-Threading / Event Loop / Callback - NOW WORKING (aquire/release GIL)

master
heck 2 years ago
parent 3f6f0fa279
commit 757f6a2997
  1. 19
      src/hello_world/_hello_world/_hello_world.cc
  2. 3
      test/test.py

@ -45,13 +45,18 @@ bool _wrapped_cxx_function() {
string trigger_callback() {
pEpLog("called");
// Cant be global, because of the dynamic nature of python
// And the "func pointer" can be changed anytime (callback override)
object module_ref = import("hello_world");
object funcref = module_ref.attr("callback_func");
string msg = call<string>(funcref.ptr(), 23, "c++");
pEpLog(msg);
return msg.c_str();
string ret = "";
try {
// Cant be global, because of the dynamic nature of python
// And the "func pointer" can be changed anytime (callback override)
PyGILState_STATE gil = PyGILState_Ensure();
object module_ref = import("hello_world");
object funcref = module_ref.attr("callback_func");
ret = call<string>(funcref.ptr(), 23, "c++");
PyGILState_Release(gil);
} catch (exception& e) { }
pEpLog(ret);
return ret.c_str();
}

@ -54,6 +54,9 @@ print(w.get_msg())
w.set_msg("New Message")
print(w.get_msg())
print('\nTest callback impl from main thread')
hello_world.trigger_callback()
print('\nTest callback thread')
hello_world.start_callback_thread()
do_python_stuff_for(2)

Loading…
Cancel
Save