python - 在 Cython 中使用字典,尤其是在 nogil 中

标签 python cython gil

我有一本字典,

my_dict = {'a':[1,2,3], 'b':[4,5] , 'c':[7,1,2])

我想在 Cython nogil 函数中使用这个字典。所以,我试图将其声明为

cdef dict cy_dict = my_dict 

到这个阶段就可以了。

现在我需要遍历 my_dict 的键,如果值在列表中,则遍历它。在 Python 中,这很容易,如下所示:

 for key in my_dict:
      if isinstance(my_dict[key], (list, tuple)):
          ###### Iterate over the value of the list or tuple
          for value in list:
               ## Do some over operation.

但是,在 Cython 中,我想在 nogil 中实现同样的功能。因为,nogil 中不允许使用 python 对象,所以我都被困在这里。

with nogil:
    #### same implementation of the same in Cython

谁能帮帮我?

最佳答案

如果没有 GIL,你就不能使用 Python dict,因为你可以用它做的所有事情都涉及到操作 Python 对象。您最明智的选择是接受您需要 GIL。还有一个不太明智的选项也涉及 C++ 映射,但它可能很难适用于您的特定情况。

您可以使用 with gil: 重新获取 GIL。这里有明显的开销(使用 GIL 的部分不能并行执行,并且可能有等待 GIL 的延迟)。然而,如果字典操作是一大段 Cython 代码的一小块,这可能还不错:

with nogil:
  # some large chunk of computationally intensive code goes here
  with gil:
    # your dictionary code
  # more computationally intensive stuff here

另一个不太明智的选择是使用 C++ 映射(以及其他 C++ 标准库数据类型)。 Cython 可以包装这些并自动转换它们。根据您的示例数据给出一个简单的示例:

from libcpp.map cimport map
from libcpp.string cimport string
from libcpp.vector cimport vector
from cython.operator cimport dereference, preincrement

def f():
    my_dict = {'a':[1,2,3], 'b':[4,5] , 'c':[7,1,2]}
    # the following conversion has an computational cost to it 
    # and must be done with the GIL. Depending on your design
    # you might be able to ensure it's only done once so that the
    # cost doesn't matter much
    cdef map[string,vector[int]] m = my_dict
    
    # cdef statements can't go inside no gil, but much of the work can
    cdef map[string,vector[int]].iterator end = m.end()
    cdef map[string,vector[int]].iterator it = m.begin()
        
    cdef int total_length = 0
    
    with nogil: # all  this stuff can now go inside nogil   
        while it != end:
            total_length += dereference(it).second.size()
            preincrement(it)
        
    print total_length

(您需要使用 language='c++' 进行编译)。

这样做的明显缺点是必须事先知道 dict 中的数据类型(它不能是任意的 Python 对象)。但是,由于您不能在 nogil block 内操作任意 Python 对象,因此您受到了很大的限制。

6 年后的补充: 我不建议将“在任何地方使用 C++ 对象”方法作为通用方法。 Cython-C++ 接口(interface)有点笨拙,您可以花很多时间来解决它。 Python 容器实际上比您想象的要好。每个人都倾向于忘记将 C++ 对象与 Python 对象相互转换的成本。 People rarely consider if they really need to release the GIL or if they just read an article on the internet somewhere saying that the GIL is bad. .

这对某些任务很有用,但在盲目地将所有 list 替换为 vector,将 dict 替换为 map 等等。通常,如果您的 C++ 类型完全存在于您的函数中,那么可能是一个很好的举措(但请三思...)。如果它们被转换为输入或输出参数,请再三考虑。

关于python - 在 Cython 中使用字典,尤其是在 nogil 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32266444/

相关文章:

python - 需要 python 字典循环帮助

python - 包 "not found as"错误中的 Cython pickle

python - numpy 和全局解释器锁

python - python中ctypes创建的线程也在GIL下吗?

python - 如何为pymc3模型中的参数添加约束?

python - 使用python发送电子邮件: how to form the message?

python - Cython:加速简单代码

python - 用 cython 声明一个 numpy 数组会奇怪地产生大量开销

python - 在某些情况下,Python 线程可以安全地操作共享状态吗?

python - 如何加快对部分 numpy 数组的迭代