How to use C extensions in python to get around GIL

后端 未结 5 1219
走了就别回头了
走了就别回头了 2021-02-19 23:51

I want to run a cpu intensive program in Python across multiple cores and am trying to figure out how to write C extensions to do this. Are there any code samples or tutorials

5条回答
  •  一个人的身影
    2021-02-20 00:30

    This is a good use of C extension. The keyword you should search for is Py_BEGIN_ALLOW_THREADS.

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

    P.S. I mean if you processing is already in C, like imaging processing, then release the lock in C extension is good. If your processing code is mainly in Python, other people's suggestion to multiprocessing is better. It is usually not justify to rewrite the code in C for background processing.

提交回复
热议问题