The main Python thread is a conversion method that we often use. But how can we better use this programming language? Next, let's take a look at this article in detail. In the main Python thread or child thread, the following processing is used to call Python functions.
- {
- Class PyThreadStateLock PyThreadLock;
- // Call python API function processing
- ......
- }
Haha, it's very easy to see.
There are also two macros related to the global lock, Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS. These two macros are used to temporarily release the global lock before calling the C function for a long time, and then obtain the global lock again to avoid blocking other python threads to continue running. These two macros can be called in this way.
- {
- Class PyThreadStateLock PyThreadLock;
- // Call python API function processing
- ......
- Py_BEGIN_ALLOW_THREADS
- // Call the C function that requires a long time
- ......
- Py_END_ALLOW_THREADS
- // Call python API function processing
- ......
- }
Possible Errors and Solutions
A. a link problem occurs in debug mode in vs 200x.
- extmodule.obj : error LNK2019: unresolved external
symbol __imp___Py_Dealloc referenced in function _
PySwigObject_format
- extmodule.obj : error LNK2019: unresolved external
symbol __imp___Py_NegativeRefcount referenced in function
_PySwigObject_format
- extmodule.obj : error LNK2001: unresolved external symbol
__imp___Py_RefTotal
- extmodule.obj : error LNK2019: unresolved external symbol
__imp___PyObject_DebugFree referenced in function
_PySwigObject_dealloc
- extmodule.obj : error LNK2019: unresolved external symbol __imp
___PyObject_DebugMalloc referenced in function _PySwigObject_New
- extmodule.obj : error LNK2019: unresolved external symbol __imp
__Py_InitModule4TraceRefs referenced in function _init_extmodule
It is mainly caused by Py_DEBUG/Py_TRACE_REFS. Modify the pyconfig. h and object. h files under Python \ include. The above is a description of the main Python thread.