Embedding python in multithreaded C application

前端 未结 3 1922
臣服心动
臣服心动 2020-12-03 10:48

I\'m embedding the python interpreter in a multithreaded C application and I\'m a little confused as to what APIs I should use to ensure thread safety.

From what I g

3条回答
  •  忘掉有多难
    2020-12-03 11:15

    Having a multi-threaded C app trying to communicate from multiple threads to multiple Python threads of a single CPython instance looks risky to me.

    As long as only one C thread communicates with Python you should not have to worry about locking even if the Python application is multi-threading. If you need multiple python threads you can set the application up this way and have multiple C threads communicate via a queue with that single C thread that farms them out to multiple Python threads.

    An alternative that might work for you is to have multiple CPython instances one for each C thread that needs it (of course communication between Python programs should be via the C program).

    Another alternative might the Stackless Python interpreter. That does away with the GIL, but I am not sure you run into other problems binding it to multiple threads. stackless was a drop-in replacement for my (single-threaded) C application.

提交回复
热议问题