gpt4 book ai didi

multithreading - 如何同时在两个不同的线程中执行 PyObject_CallObject()?

转载 作者:行者123 更新时间:2023-12-04 04:29:50 27 4
gpt4 key购买 nike

我有两个函数同时在两个线程上运行。两个函数都调用此方法 PyObject_CallObject(pFunc,pArgs)执行两个不同的 python 函数。但是当我尝试这样做时,我遇到了 access violation reading location 异常。两个函数中使用的 pFuncpArgs 都是该函数的本地数据,没有共享数据。我仍然如何得到异常(exception)?

最佳答案

那是不可能的。在调用任何 Python CAPI 函数之前,您必须获取全局解释器锁 (GIL)。只有少数函数可以在没有 GIL 的情况下调用。

https://docs.python.org/2/c-api/init.html#thread-state-and-the-global-interpreter-lock

The Python interpreter is not fully thread-safe. In order to support multi-threaded Python programs, there’s a global lock, called the global interpreter lock or GIL, that must be held by the current thread before it can safely access Python objects. Without the lock, even the simplest operations could cause problems in a multi-threaded program: for example, when two threads simultaneously increment the reference count of the same object, the reference count could end up being incremented only once instead of twice.

关于multithreading - 如何同时在两个不同的线程中执行 PyObject_CallObject()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26115414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com