在 Python 中,如果我使用的 C 扩展不是线程安全的,我可以使代码线程安全吗?
我的 Python 代码有奇怪的错误,我想知道这是否可能是由于使用线程不安全的 C 扩展引起的。 是否可以使用一些 Python 功能(例如“lock”)来解决 C 扩展问题?我怎样才能实现这个?
[编辑1] 这是我的代码的样子:
import CextensionPackage as cext
try:
(output) = cext.extension_function(inputs)
except:
pass
[编辑3] 包 init.py 文件如下所示:
from c_ext import functionx
def extension_function( ... )
...
outputs = functionx( ... )
然后 C 扩展 c_ext.c 如下所示:
#include <Python.h>
#include <cext.h>
static PyObject *functionx(PyObject *self, PyObject *args)
{
char *string;
int stringsize;
if (!PyArg_ParseTuple(args, "s#", &string, &stringsize))
return NULL;
outputs = thread_unsafe_C_function(string);
[编辑 2] 基本上,我想强制 cext.extension_function 或 thread_unsafe_C_function (以及它在 C 扩展库中调用的所有函数)在单个线程上执行。我认为这可以解决我的问题,但我该怎么做呢? 感谢您的帮助。
My Python code have strange bugs, and I wonder if it could be caused by using a thread unsafe C extension.
Would it be possible to use some Python features, like "lock", to fix that C extension problem? And how could I implement this?
[edit 1]
Here's what my code looks like:
import CextensionPackage as cext
try:
(output) = cext.extension_function(inputs)
except:
pass
[edit 3]
The package init.py file looks like:
from c_ext import functionx
def extension_function( ... )
...
outputs = functionx( ... )
Then the C extension c_ext.c looks like:
#include <Python.h>
#include <cext.h>
static PyObject *functionx(PyObject *self, PyObject *args)
{
char *string;
int stringsize;
if (!PyArg_ParseTuple(args, "s#", &string, &stringsize))
return NULL;
outputs = thread_unsafe_C_function(string);
[edit 2]
Basically, I would like to force cext.extension_function, or the thread_unsafe_C_function (and all the functions it calls in the C extension library) to execute on a single thread. I think that would solve my problem, but how can I do that?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您认为您的 C 扩展不是线程安全的,那么您可以使用 Python 同步原语(如锁)来确保扩展中一次只有一个线程正在执行。我无法知道这是否能帮助您解决问题。
If you think your C extension is not thread-safe, then you can use Python synchronization primitives (like a lock) to ensure that only one thread is executing in the extension at a time. There's no way for me to know if that will help you with your problems.
如果 c 扩展代码做了一些愚蠢的事情,我认为你别无选择,只能选择使用或不使用它。唯一的例外是您有权访问代码并且可以确保它执行您想要的操作。
C 代码可能会不适当地释放 GIL,或者在内部创建自己的线程。
您能采取什么措施来确保在没有扩展的情况下问题不存在(确保错误不在您这边)?也许对问题的更多解释会有所帮助,但如果我不信任扩展,我就不会使用它。我不认为你可以在没有来源的情况下强迫它做你想做的事。
[编辑]我们需要查看更多代码。输出参数会发生什么变化?另外,你如何创建额外的线程?
If the c extension code does something stupid, I don't think you have any choice but to choose to use it or not. Only exception would be if you have access to the code and can make sure it does what you want.
The c code could release the GIL inappropriately, or create its own threads internally.
Can you do anything to make sure the problem don't exist without the extension (make sure the bug isn't on your side)? Maybe more explanation of the question would help, but if I didn't trust an extension, I just wouldn't use it. I don't think you can force it to do what you want without source.
[edit]We need to see more of the code. What happens to the outputs parameter? Also, how are you creating additional threads?