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

后端 未结 2 1273
借酒劲吻你
借酒劲吻你 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:27

    Can I access data in a Python object while the thread does not own the GIL?

    No you cannot.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题