Threading
Hello, I'm trying to call Python routines from C/C++. These routines must intereact in a multi-thread environment. I mean that routine A will loop waiting for a condition of routine B. This condition are coded in C so I don't need routine A running in the same interpreter of routine B. I tried using: PyEval_AcquireLock(); PyInterpreterState* mainInterpreterState = mainThreadState->interp; PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState); PyThreadState_Swap(myThreadState); ... Call Python code here PyThreadState_Swap(NULL); PyEval_ReleaseLock(); Obviosly, I called thread initialization code in the main thread like http://www.linuxjournal.com/article/3641 The problem with this code is that I hold the interpreter lock when calling routine A or B so they cannot run simultaneous and that is a requierement for me (as I explained before). It executes some code of the routine I called but then it hungs. If I release the lock before calling the Python code it doesn´t work neither somhow. Then I tried to create an interpreter for each thread. It is not desirable but it's acceptable: PyEval_AcquireLock(); PyThreadState* myThreadState = Py_NewInterpreter(); PyThreadState_Swap(myThreadState); PyEval_ReleaseLock(); ... Call Python code here PyEval_AcquireLock(); PyThreadState_Swap(NULL); Py_EndInterpreter(myThreadState); PyEval_ReleaseLock(); But it doesn't work. It seems that it requieres to take the lock to call Python code. If I comment the PyEval_ReleaseLock line and PyEval_AcquireLock pair it calls other threads before hunging. I saw some threads talking about some similar situations and I saw no answer to this. This issue remains unsolved? Thanks on advance, Pablo -- http://www.nektra.com -- http://mail.python.org/mailman/listinfo/python-list
C++ Binding with Threads
Hello, I want to embed Python in an application and use an API of the application from Python. The application uses a library that creates several threads and I the users of the application will write Python scripts handling this events. The problem is that I having problems with threads. I saw that I have to PyEval_InitThreads and then PyThreadState_New and PyThreadState_Swap from the new thread but this way to solve the problem doesn't work for me because I need to let 2 or more threads run at the SAME time. The interpreter lock doesn't let them run at the same time so I'm looking for another solution. I saw Py_NewInterpreter and I tried to use it but it doesn't work if I don't keep the lock. Can anyone help me to solve this issue or tell me 'Forget it!'? Thanks on advance, Pablo Yabo -- http://mail.python.org/mailman/listinfo/python-list
C++ Binding with Threads
Hello, I want to embed Python in an application and use an API of the application from Python. The application uses a library that creates several threads and I the users of the application will write Python scripts handling this events. The problem is that I having problems with threads. I saw that I have to PyEval_InitThreads and then PyThreadState_New and PyThreadState_Swap from the new thread but this way to solve the problem doesn't work for me because I need to let 2 or more threads run at the SAME time. The interpreter lock doesn't let them run at the same time so I'm looking for another solution. I saw Py_NewInterpreter and I tried to use it but it doesn't work if I don't keep the lock. Can anyone help me to solve this issue or tell me 'Forget it!'? Thanks on advance, Pablo Yabo -- http://www.nektra.com -- http://mail.python.org/mailman/listinfo/python-list
Re: C++ Binding with Threads
I've found a workaround for me project that I'll share. The solution is to release the interpreter lock when a function of my wapper is called, I mean from Python a function of C++ is called so I released the interpreter lock and take it before returning to Python. It's important to provide Sleep functions in your C++ code. Otherwise, multi-threading will not work at all. If most of the work is done in your C++ code, using this workaround will solve the issue. To make it easier I've wrote a class to create an instance at the beginning of each wrapped function. Here is the constructor / destructor: PythonAutoLock::PythonAutoLock() { PythonMgr *mgr = PythonMgr::instance(); _state = PyThreadState_Get(); PyThreadState_Swap(NULL); PyEval_ReleaseLock(); } PythonAutoLock::~PythonAutoLock() { PythonMgr *mgr = PythonMgr::instance(); PyEval_AcquireLock(); PyThreadState_Swap(_state); } Pablo Yabo -- http://www.nektra.com On 8/13/07, Pablo Yabo <[EMAIL PROTECTED]> wrote: > > Hello, > > I want to embed Python in an application and use an API of the application > from Python. > The application uses a library that creates several threads and I the > users of the application will write Python scripts handling this events. > > The problem is that I having problems with threads. I saw that I have to > PyEval_InitThreads and then PyThreadState_New and PyThreadState_Swap from > the new thread but this way to solve the problem doesn't work for me because > I need to let 2 or more threads run at the SAME time. > The interpreter lock doesn't let them run at the same time so I'm looking > for another solution. I saw Py_NewInterpreter and I tried to use it but it > doesn't work if I don't keep the lock. > > Can anyone help me to solve this issue or tell me 'Forget it!'? > > > Thanks on advance, > Pablo Yabo > -- > http://www.nektra.com > -- http://mail.python.org/mailman/listinfo/python-list
Thread structures BUG?
Hello, I create lots of threads that run a script and I have a problem when a thread is created with the same threadId that existed before. When a new thread is started I use this code to enter the interpreter: // create a thread state object for this thread PyEval_AcquireLock(); threadState = PyThreadState_New(mainInterpreterState); PyThreadState_Swap(threadState); and when the thread finishes: PyThreadState_Swap(NULL); PyThreadState_Clear(info); // delete my thread state object PyThreadState_Delete(info); PyEval_ReleaseLock(); Everything works smoothly until a thread id is re-used. I've been testing what is happening and the problem is that PyGILState_GetThisThreadState() returns a PyThreadState for the reused thread id. I changed my code to call PyGILState_GetThisThreadState() before creating the PyThreadState. But if I try to use the returned PyThreadState (that shouldn't exist because I've already deleted) I got an error when trying to access the interpreter dictionary. The workaround that I found is to keep all PyThreadState structures without calling PyThreadState_Clear / PyThreadState_Delete. When a new thread is created I call PyGILState_GetThisThreadState(). If it returns something I reuse the structure. In this way, my code works. Thanks, Pablo Yabo -- http://www.nektra.com -- http://mail.python.org/mailman/listinfo/python-list