Global Interpreter Lock and access to data (eg. for NumPy arrays)

后端 未结 2 1280
借酒劲吻你
借酒劲吻你 2020-12-16 05:11

I am writing a C extension for Python, which should release the Global Interpreter Lock while it operates on data. I think I have understood the mechanism of the GIL fairly

2条回答
  •  星月不相逢
    2020-12-16 05:40

    Is this safe?

    Strictly, no. I think you should move the calls to PyArray_SIZE and PyArray_DATA outside the GIL-less block; if you do that, you'll be operating on C data only. You might also want to increment the reference count on the object before going into the GIL-less block and decrement it afterwards.

    After your edits, it should be safe. Don't forget to decrement the reference count afterwards.

提交回复
热议问题