Call Python from C++

前端 未结 2 454
一整个雨季
一整个雨季 2020-12-16 04:42

I\'m trying to call a function in a Python script from my main C++ program. The python function takes a string as the argument and returns nothing (ok.. \'None\'). It works

2条回答
  •  清酒与你
    2020-12-16 05:28

    When you say "as long as the previous call is finished before the function is called again", I can only assume that you have multiple threads calling from C++ into Python. The python is not thread safe, so this is going to fail!

    Read up on the Global Interpreter Lock (GIL) in the Python manual. Perhaps the following links will help:

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

    The GIL is mentioned on Wikipedia:

    • http://en.wikipedia.org/wiki/Global_Interpreter_Lock

提交回复
热议问题