从Python代码返回C ++时,Python PyGILState_ {Ensure / Release}导致段错误

发布于 2021-01-29 16:58:58

更新
嗯,看起来好像在调用PyGILState_Ensure()之前就添加了PyEval_InitThreads()。我急于想办法,将我的“悬挂”错误地归因于PyEval_InitThreads()。

但是,在阅读了一些Python文档之后,我想知道这是否是正确的解决方案。

当未知哪个线程(如果有)当前具有全局解释器锁时,调用此函数是不安全的。


首先,我正在研究一些修改的GNU Radio代码-
特别是修改的gr_bin_statistics_f块。现在,有一个错误报告(尽管是一个旧报告),几乎可以描述我的确切情况。

http://gnuradio.org/redmine/issues/show/199

现在,在错误报告中提到的usrp_spectrum_sense.py调用gr_bin_statistics_f(C
++),然后定期调用Python以重新调整USRP(无​​线电)。

调用Python代码时,会发生以下情况:

PyGILState_STATE d_gstate;
d_gstate = PyGILState_Ensure();

// call python code

PyGILState_Release(d_gstate);

因此,一旦我们从Python代码返回,则在调用PyGILState_Release(d_gstate)时会发生分段错误。尽管我的代码与原始的gr_bin_statistics_f之间存在差异,但似乎没有任何远程关系。

我读到在PyGILState_Ensure()解决某些问题之前调用PyEval_InitThreads(),但这只会导致程序挂起。

谁能为我阐明这一点?还是只是将消息发送到GNU Radio邮件列表的时间?

在Fedora 14 x86_64上使用Python2.7。

这是GDB回溯:

(gdb) c
Continuing.
[New Thread 0x7fabd3a8d700 (LWP 23969)]
[New Thread 0x7fabd328c700 (LWP 23970)]
[New Thread 0x7fabd2a8b700 (LWP 23971)]
[New Thread 0x7fabd228a700 (LWP 23972)]
[New Thread 0x7fabd1a89700 (LWP 23973)]
[New Thread 0x7fabd1288700 (LWP 23974)]
[New Thread 0x7fabd0a87700 (LWP 23975)]
[New Thread 0x7fabbbfff700 (LWP 23976)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fabbbfff700 (LWP 23976)]
0x00000036b3e0db00 in sem_post () from /lib64/libpthread.so.0
(gdb) bt
#0  0x00000036b3e0db00 in sem_post () from /lib64/libpthread.so.0
#1  0x00000036c1317679 in PyThread_release_lock () from /usr/lib64/libpython2.7.so.1.0
#2  0x00007fabd6159c1f in ~ensure_py_gil_state (this=0x2dc6fc0, x=887000000)
    at gnuradio_swig_py_general.cc:5593
#3  gr_py_feval_dd::calleval (this=0x2dc6fc0, x=887000000) at gnuradio_swig_py_general.cc:5605
#4  0x00007fabd77c4b6e in gr_noise_level_f::tune_window (this=0x2db3ca0, 
    target_freq=) at gr_noise_level_f.cc:97
#5  0x00007fabd77c554b in gr_noise_level_f::work (this=0x2db3ca0, noutput_items=7, 
    input_items=, output_items=)
    at gr_noise_level_f.cc:115
#6  0x00007fabd7860714 in gr_sync_block::general_work (this=0x2db3ca0, 
    noutput_items=, ninput_items=, 
    input_items=, output_items=) at gr_sync_block.cc:64
#7  0x00007fabd7846ce4 in gr_block_executor::run_one_iteration (this=0x7fabbbffed90)
    at gr_block_executor.cc:299
#8  0x00007fabd7864332 in gr_tpb_thread_body::gr_tpb_thread_body (this=0x7fabbbffed90, block=...)
    at gr_tpb_thread_body.cc:49
#9  0x00007fabd785cce7 in operator() (function_obj_ptr=...) at gr_scheduler_tpb.cc:42
#10 operator() (function_obj_ptr=...)
    at /home/tja/Research/energy/detector/gnuradio-3.3.0/gruel/src/include/gruel/thread_body_wrapper.h:49
#11 boost::detail::function::void_function_obj_invoker0, void>::invoke (function_obj_ptr=...) at /usr/include/boost/function/function_template.hpp:153
---Type  to continue, or q  to quit---
#12 0x00007fabd74914ef in operator() (this=)
    at /usr/include/boost/function/function_template.hpp:1013
#13 boost::detail::thread_data >::run (this=)
    at /usr/include/boost/thread/detail/thread.hpp:61
#14 0x00007fabd725ca55 in thread_proxy () from /usr/lib64/libboost_thread-mt.so.1.44.0
#15 0x00000036b3e06d5b in start_thread () from /lib64/libpthread.so.0
#16 0x00000036b3ae4a7d in clone () from /lib64/libc.so.6
(gdb)

感谢您的光临!

关注者
0
被浏览
101
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    Python期望在尝试从子线程回调之前,主线程进行一定数量的初始化。

    如果主线程是嵌入Python的应用程序,则应PyEval_InitThreads()在调用后立即调用Py_Initialize()

    如果主线程是Python解释器本身(如此处所示),则使用多线程扩展模块的模块应尽早包含“导入线程”,以确保PyEval_InitThreads()在产生任何子线程之前可以正确调用该线程。



推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看