PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

前端 未结 7 1009
别那么骄傲
别那么骄傲 2020-11-27 03:06

Basically there seems to be massive confusion/ambiguity over when exactly PyEval_InitThreads() is supposed to be called, and what accompanying API

相关标签:
7条回答
  • 2020-11-27 03:57

    I feel confuse on this issue too. The following code works by coincidence.

    Py_InitializeEx(0);
        if (!PyEval_ThreadsInitialized()) {
        PyEval_InitThreads();
        PyThreadState* mainPyThread = PyEval_SaveThread();
    }
    

    My main thread do some python runtime initial work, and create other pthread to handle tasks. And I have a better workaround for this. In the Main thread:

    if (!PyEval_ThreadsInitialized()){
        PyEval_InitThreads();
    }
    //other codes
    while(alive) {
        Py_BEGIN_ALLOW_THREADS
        sleep or other block code
        Py_END_ALLOW_THREADS
    }
    
    0 讨论(0)
提交回复
热议问题