[英]Boost.Python and Boost.Signals2: Segmentation faults
我在將我的boost.python公開的現有C ++庫中集成boost.signals2時遇到問題。
我有一個使用std::shared_ptr
公開給python的類。 該類應該能夠在某些事件上發出一些信號。 因此,我公開了一個connect_slot
函數,該函數以boost::python::object
作為參數。 如果我在連接插槽后立即發出信號,則一切正常,但是如果稍后再上課,則收到分段錯誤。
我認為這可能與c ++ lib中的線程有關(它也使用boos :: asio等)。
以下是一些代碼段:
MyClass.h:
public:
typedef boost::signals2::signal<void (std::shared_ptr<int>)> signal_my_sig;
void connect_slot(boost::python::object const & slot);
private:
signal_my_sig m_sig;
MyClass.cpp:
void MyClass::connect_slot(boost::python::object const & slot) {
std::cout << "register shd" << std::endl;
m_sig.connect(slot);
m_sig(12345); // this works
}
void MyClass::some_later_event() {
m_sig(654321); // this does not work
}
我用這樣的自定義python函數在python中調用MyClass :: connect_slot函數:
def testfunc(some_int):
print("slot called")
m = myext.MyClass()
m.connect_slot(testfunc)
MyClass::some_later_event
引發的分段錯誤的回溯(使用gdb)如下所示:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff3c37700 (LWP 20634)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff3c37700 (LWP 20634)]
0x00000000004f7480 in PyObject_Call ()
(gdb)
(gdb) backtrace
#0 0x00000000004f7480 in PyObject_Call ()
#1 0x00000000004f7aa6 in PyEval_CallObjectWithKeywords ()
#2 0x000000000049bd84 in PyEval_CallFunction ()
#3 0x00007ffff5375d9f in boost::python::call<boost::python::api::object, int>
(callable=0x7ffff7ed4578, a0=@0x7ffff3c35b34: 5)
at /usr/local/boost_1_55_0/boost/python/call.hpp:66
#4 0x00007ffff5374b81 in boost::python::api::object_operators<boost::python::api::object>::operator()<int> (this=0x9e3bf0, a0=@0x7ffff3c35b34: 5)
at /usr/local/boost_1_55_0/boost/python/object_call.hpp:19
#5 0x00007ffff5373658 in boost::detail::function::void_function_obj_invoker1<boost::python::api::object, void, int>::invoke (function_obj_ptr=..., a0=5)
at /usr/local/boost_1_55_0/boost/function/function_template.hpp:153
#6 0x00007ffff5378a3c in boost::function1<void, int>::operator() (
this=0x9e3be8, a0=5)
at /usr/local/boost_1_55_0/boost/function/function_template.hpp:767
#7 0x00007ffff53781f9 in boost::signals2::detail::call_with_tuple_args<boost::signals2::detail::void_type>::m_invoke<boost::function<void (int)>, 0u, int&>(void*, boost::function<void (int)>&, boost::signals2::detail::unsigned_meta_array<0u>, std::tuple<int&>) const (this=0x7ffff3c35c7f, func=..., args=...)
at /usr/local/boost_1_55_0/boost/signals2/detail/variadic_slot_invoker.hpp:92
有任何想法嗎?
如果從未顯式管理全局解釋器鎖 (GIL)的C ++線程調用MyClass::some_later_event()
,則可能導致未定義的行為。
讓我們考慮一下C ++線程與Python交互的情況。 例如,可以將C ++線程設置為通過MyClass.event_in(seconds, value)
在一段時間后調用MyClass
的信號。
這個例子可能會涉及很多,所以讓我們從基礎開始:Python的GIL。 簡而言之,GIL是解釋器周圍的互斥體。 如果線程做的任何事情都會影響python受管理對象的引用計數,則它需要獲取GIL。 在GDB追溯中,Boost.Signals2庫可能試圖在沒有GIL的情況下調用Python對象,從而導致崩潰。 盡管管理GIL非常簡單,但它可能很快變得復雜。
首先,模塊需要讓Python初始化GIL以進行線程化。
BOOST_PYTHON_MODULE(example)
{
PyEval_InitThreads(); // Initialize GIL to support non-python threads.
// ...
}
為了方便起見,讓我們創建一個簡單的類來幫助通過范圍管理GIL:
/// @brief RAII class used to lock and unlock the GIL.
class gil_lock
{
public:
gil_lock() { state_ = PyGILState_Ensure(); }
~gil_lock() { PyGILState_Release(state_); }
private:
PyGILState_STATE state_;
};
讓我們確定C ++線程何時需要GIL:
boost::signals2::signal
可以創建連接對象的其他副本,就像在同時調用信號時所做的那樣。 boost::signals2::signal
連接的Python對象。 回調肯定會影響python對象。 例如,提供給__call__
方法的self
參數將增加和減少對象的引用計數。 MyClass
類。 這是一個基於原始代碼的基本模型類:
/// @brief Mockup class.
class MyClass
{
public:
/// @brief Connect a slot to the signal.
template <typename Slot>
void connect_slot(const Slot& slot)
{
signal_.connect(slot);
}
/// @brief Send an event to the signal.
void event(int value)
{
signal_(value);
}
private:
boost::signals2::signal<void(int)> signal_;
};
由於C ++線程可能正在調用MyClass
的信號,因此MyClass
的生存期必須至少與線程一樣長。 一個很好的候選方法是讓Boost.Python使用boost::shared_ptr
管理MyClass
。
BOOST_PYTHON_MODULE(example)
{
PyEval_InitThreads(); // Initialize GIL to support non-python threads.
namespace python = boost::python;
python::class_<MyClass, boost::shared_ptr<MyClass>,
boost::noncopyable>("MyClass")
.def("event", &MyClass::event)
// ...
;
}
boost::signals2::signal
與python對象進行交互。 boost::signals2::signal
在被調用時可能會復制。 另外,可能有C ++插槽連接到信號,因此,理想的是僅在調用Python插槽時鎖定GIL。 但是, signal
沒有提供鈎子來允許我們在創建插槽副本或調用插槽之前獲取GIL。
為了避免產生signal
創建boost::python::object
插槽的副本,可以使用包裝類來創建boost::python::object
的副本,以便引用計數保持准確,並通過shared_ptr
管理副本。 這允許signal
自由地創建shared_ptr
副本,而不是在沒有GIL的情況下復制boost::python::object
。
該GIL安全插槽可以封裝在幫助程序類中。
/// @brief Helepr type that will manage the GIL for a python slot.
///
/// @detail GIL management:
/// * Caller must own GIL when constructing py_slot, as
/// the python::object will be copy-constructed (increment
/// reference to the object)
/// * The newly constructed python::object will be managed
/// by a shared_ptr. Thus, it may be copied without owning
/// the GIL. However, a custom deleter will acquire the
/// GIL during deletion.
/// * When py_slot is invoked (operator()), it will acquire
/// the GIL then delegate to the managed python::object.
struct py_slot
{
public:
/// @brief Constructor that assumes the caller has the GIL locked.
py_slot(const boost::python::object& object)
: object_(
new boost::python::object(object), // GIL locked, so copy.
[](boost::python::object* object) // Delete needs GIL.
{
gil_lock lock;
delete object;
}
)
{}
// Use default copy-constructor and assignment-operator.
py_slot(const py_slot&) = default;
py_slot& operator=(const py_slot&) = default;
template <typename ...Args>
void operator()(Args... args)
{
// Lock the GIL as the python object is going to be invoked.
gil_lock lock;
(*object_)(args...);
}
private:
boost::shared_ptr<boost::python::object> object_;
};
Python將公開一個輔助函數,以幫助修改類型。
/// @brief MyClass::connect_slot helper.
template <typename ...Args>
void MyClass_connect_slot(
MyClass& self,
boost::python::object object)
{
py_slot slot(object); // Adapt object to a py_slot for GIL management.
// Using a lambda here allows for the args to be expanded automatically.
// If bind was used, the placeholders would need to be explicitly added.
self.connect_slot([slot](Args... args) mutable { slot(args...); });
}
更新的綁定公開了輔助函數:
python::class_<MyClass, boost::shared_ptr<MyClass>,
boost::noncopyable>("MyClass")
.def("connect_slot", &MyClass_connect_slot<int>)
.def("event", &MyClass::event)
// ...
;
線程的功能是相當基本的:它先休眠然后調用信號。 但是,了解GIL的上下文很重要。
/// @brief Sleep then invoke an event on MyClass.
template <typename ...Args>
void MyClass_event_in_thread(
boost::shared_ptr<MyClass> self,
unsigned int seconds,
Args... args)
{
// Sleep without the GIl.
std::this_thread::sleep_for(std::chrono::seconds(seconds));
// We do not want to hold the GIL while invoking or copying
// C++-specific slots connected to the signal. Thus, it is the
// responsibility of python slots to manage the GIL via the
// py_slot wrapper class.
self->event(args...);
}
/// @brief Function that will be exposed to python that will create
/// a thread to call the signal.
template <typename ...Args>
void MyClass_event_in(
boost::shared_ptr<MyClass> self,
unsigned int seconds,
Args... args)
{
// The caller may or may not have the GIL. Regardless, spawn off a
// thread that will sleep and then invoke an event on MyClass. The
// thread will not be joined so detach from it. Additionally, as
// shared_ptr is thread safe, copies of it can be made without the
// GIL.
std::thread(&MyClass_event_in_thread<Args...>, self, seconds, args...)
.detach();
}
請注意, MyClass_event_in_thread
可以表示為lambda,但是在lambda中解MyClass_event_in_thread
模板包在某些編譯器上不起作用。
並更新了MyClass
綁定。
python::class_<MyClass, boost::shared_ptr<MyClass>,
boost::noncopyable>("MyClass")
.def("connect_slot", &MyClass_connect_slot<int>)
.def("event", &MyClass::event)
.def("event_in", &MyClass_event_in<int>)
;
最終的解決方案如下所示:
#include <thread> // std::thread, std::chrono
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signals2/signal.hpp>
/// @brief Mockup class.
class MyClass
{
public:
/// @brief Connect a slot to the signal.
template <typename Slot>
void connect_slot(const Slot& slot)
{
signal_.connect(slot);
}
/// @brief Send an event to the signal.
void event(int value)
{
signal_(value);
}
private:
boost::signals2::signal<void(int)> signal_;
};
/// @brief RAII class used to lock and unlock the GIL.
class gil_lock
{
public:
gil_lock() { state_ = PyGILState_Ensure(); }
~gil_lock() { PyGILState_Release(state_); }
private:
PyGILState_STATE state_;
};
/// @brief Helepr type that will manage the GIL for a python slot.
///
/// @detail GIL management:
/// * Caller must own GIL when constructing py_slot, as
/// the python::object will be copy-constructed (increment
/// reference to the object)
/// * The newly constructed python::object will be managed
/// by a shared_ptr. Thus, it may be copied without owning
/// the GIL. However, a custom deleter will acquire the
/// GIL during deletion.
/// * When py_slot is invoked (operator()), it will acquire
/// the GIL then delegate to the managed python::object.
struct py_slot
{
public:
/// @brief Constructor that assumes the caller has the GIL locked.
py_slot(const boost::python::object& object)
: object_(
new boost::python::object(object), // GIL locked, so copy.
[](boost::python::object* object) // Delete needs GIL.
{
gil_lock lock;
delete object;
}
)
{}
// Use default copy-constructor and assignment-operator.
py_slot(const py_slot&) = default;
py_slot& operator=(const py_slot&) = default;
template <typename ...Args>
void operator()(Args... args)
{
// Lock the GIL as the python object is going to be invoked.
gil_lock lock;
(*object_)(args...);
}
private:
boost::shared_ptr<boost::python::object> object_;
};
/// @brief MyClass::connect_slot helper.
template <typename ...Args>
void MyClass_connect_slot(
MyClass& self,
boost::python::object object)
{
py_slot slot(object); // Adapt object to a py_slot for GIL management.
// Using a lambda here allows for the args to be expanded automatically.
// If bind was used, the placeholders would need to be explicitly added.
self.connect_slot([slot](Args... args) mutable { slot(args...); });
}
/// @brief Sleep then invoke an event on MyClass.
template <typename ...Args>
void MyClass_event_in_thread(
boost::shared_ptr<MyClass> self,
unsigned int seconds,
Args... args)
{
// Sleep without the GIL.
std::this_thread::sleep_for(std::chrono::seconds(seconds));
// We do not want to hold the GIL while invoking or copying
// C++-specific slots connected to the signal. Thus, it is the
// responsibility of python slots to manage the GIL via the
// py_slot wrapper class.
self->event(args...);
}
/// @brief Function that will be exposed to python that will create
/// a thread to call the signal.
template <typename ...Args>
void MyClass_event_in(
boost::shared_ptr<MyClass> self,
unsigned int seconds,
Args... args)
{
// The caller may or may not have the GIL. Regardless, spawn off a
// thread that will sleep and then invoke an event on MyClass. The
// thread will not be joined so detach from it. Additionally, as
// shared_ptr is thread safe, copies of it can be made without the
// GIL.
// Note: MyClass_event_in_thread could be expressed as a lambda,
// but unpacking a template pack within a lambda does not work
// on some compilers.
std::thread(&MyClass_event_in_thread<Args...>, self, seconds, args...)
.detach();
}
BOOST_PYTHON_MODULE(example)
{
PyEval_InitThreads(); // Initialize GIL to support non-python threads.
namespace python = boost::python;
python::class_<MyClass, boost::shared_ptr<MyClass>,
boost::noncopyable>("MyClass")
.def("connect_slot", &MyClass_connect_slot<int>)
.def("event", &MyClass::event)
.def("event_in", &MyClass_event_in<int>)
;
}
還有一個測試腳本:
from time import sleep
import example
def spam1(x):
print "spam1: ", x
def spam2(x):
print "spam2: ", x
c = example.MyClass()
c.connect_slot(spam1)
c.connect_slot(spam2)
c.event(123)
print "Sleeping"
c.event_in(3, 321)
sleep(5)
print "Done sleeping"
結果如下:
spam1: 123
spam2: 123
Sleeping
spam1: 321
spam2: 321
Done sleeping
感謝Tanner Sansbury鏈接到他對此帖子的回答。 這解決了我的問題,除了我無法調用接受參數的信號。
我通過編輯py_slot類解決了這個問題:
struct py_slot {
public:
/// @brief Constructor that assumes the caller has the GIL locked.
py_slot(const boost::python::object& object)
: object_(new boost::python::object(object), // GIL locked, so copy.
py_deleter<boost::python::object>()) // Delete needs GIL.
{}
void operator()(SomeParamClass param) {
// Lock the gil as the python object is going to be invoked.
gil_lock lock;
(*object_)(param);
private:
boost::shared_ptr<boost::python::object> object_;
};
boost :: bind調用看起來像這樣:
self->connect_client_ready(boost::bind(&py_slot<SomeParamClass>::operator(), py_slot<SomeParamClass>(object), _1)); // note the _1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.