1

I have written a python extension wrapping an existing C++ library live555 (wrapping RTSP client interface to be specific) in SWIG. The extension works when it is operated in a single thread, but as soon as I call the event loop function of the library, python interpreter never gets the control back. So if I create a scheduled task using threading.Timer right before calling the event loop, that task never gets executed once event loop starts. To fix this issue, I added Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS macros manually in the SWIG auto generated wrapper cxx file around every doEventLoop() function call. But now, I want to do the same (i.e. allow threads) when SWIG generates the code itself and not to change any code manually. Has anyone done something similar in SWIG?

P.S. - I would also consider switching to any other framework (like SIP) to get this working. I selected SWIG over any other technology is because writing SWIG interface was really very easy and I just had to include the existing header files.

4

1 回答 1

5

SWIG 为您提供了大量的钩子来帮助实现这一目标。如果粗略的解决方案足以满足您的需求,我过去做过的一件事就是在我的 .swig 文件中放入类似这样的内容:

%exception {
    Py_BEGIN_ALLOW_THREADS
    $action
    Py_END_ALLOW_THREADS
}

这(ab)使用 SWIG 工具来装饰 C 函数调用,使用某种错误处理逻辑,以便使用 GIL 解锁/锁定来装饰这些调用。有关此处发生的情况的详细信息,请参阅SWIG 文档中的%exception 异常处理。

于 2010-03-24T23:22:06.637 回答