今天在一個程式中,在不同的兩個函數,先用Py_Initialize()初始化,然後用Py_Finalize()結束,在本機都調試沒有問題,然後將DLL發給同事後,老是在Py_Initialize()進入死迴圈,莫名其妙!怎麼調試都沒有用,奇怪了,然後將前一次的Py_Finalize()和後一次Py_Initialize()的取消,也就是僅僅經過一次的Py_Initialize(),Py_Finalize()就完全沒有問題。
這點也很奇怪,畢竟本機調試是沒有任何問題的,在同事那裡雖然換成了多線程環境,但是也沒有道理總是必在Py_Initialize()死掉吧?看了Python的文檔,才發現Py_Finalize()函數後有這麼一段
Bugs and caveats: The destruction of modules and objects in
modules is done in random order; this may cause destructors (__del__() methods) to fail when they depend on other objects
(even functions) or modules. Dynamically loaded extension modules loaded by
Python are not unloaded. Small amounts of memory allocated by the Python
interpreter may not be freed (if you find a leak, please report it). Memory tied
up in circular references between objects is not freed. Some memory allocated by
extension modules may not be freed. Some extensions may not work properly if
their initialization routine is called more than once; this can happen if an
application calls Py_Initialize() and Py_Finalize() more than once.
難道是這個問題?我這裡也不是extensions不工作的問題啊。。。。。。。或者還是多線程的問題?莫名奇妙啊。。。。。。
這裡順便記個關於Python在對線程環境下工作的文章,明天去好好研究一下是什麼問題。
C++調用PythonAPI線程狀態和全域解譯器鎖
http://blog.csdn.net/marising/archive/2008/10/08/3032278.aspx