Python Embedding Thread

O

otr2

Hi :)

I want to run Python in my app. That works still fine. But my app
supports now Threads and I would like to know what to do, that it runs
without problems.

PyGILState_Release and PyGILState_Ensure should solve the problem
right? Where do I have to put this two commands around? between each
Command that increase, decrease a reference?



Currently, this is a small code-snippet that represents the part of
the code-execution:






PyObject *f_globals=NULL, *f_locals=NULL, *mod=NULL, *rv=NULL;

mod = PyImport_ImportModule("__main__");
if (!mod)
return;

f_globals = PyModule_GetDict(mod);
f_locals = PyDict_New();
PyDict_Update(f_locals, f_globals);
if (!f_locals) {
Py_DECREF(mod);
PyErr_Print();
return;
}



rv = PyRun_String( <my code> , Py_file_input, f_locals,
f_locals);
if (!rv)
PyErr_Print();


Py_XDECREF(rv);
Py_DECREF(mod);
Py_DECREF(f_locals);
Py_DECREF(f_locals);






Thanks a lot for your help :)
 
B

Benjamin

Hi :)

I want to run Python in my app. That works still fine. But my app
supports now Threads and I would like to know what to do, that it runs
without problems.

PyGILState_Release and PyGILState_Ensure should solve the problem
right? Where do I have to put this two commands around? between each
Command that increase, decrease a reference?

Two threads should not be running through the Python VM concurrently
in the same process. The GIL has to be held *any* time you use the
Python API. When you want to release the GIL (to process something in
C), use PY_BEGIN_ALLOW_THREADS and PY_END_ALLOW_THREADS around the
places where threads can run.
 
J

Jaimy Azle

Benjamin said:
Two threads should not be running through the Python VM concurrently
in the same process. The GIL has to be held *any* time you use the
Python API. When you want to release the GIL (to process something in
C), use PY_BEGIN_ALLOW_THREADS and
PY_END_ALLOW_THREADS around the
places where threads can run.

I think he is trying to call python from thread, according to the
documentation:

"Beginning with version 2.3, threads can now take advantage of the
PyGILState_*() functions to do all of the above automatically."
- http://docs.python.org/api/threads.html

I use it everywhere on my multithreaded server without problem.

Salam,

-Jaimy
 
G

googler.1.webmaster

Hi!

Thank you very much for your answers. I have a menue with a script in
it.
So my app starts a new thread for each script.

So I would like to run two scripts all the same time. Could someone
give me a tip,
what I have to set in my code?

Thank you :)
 
G

googler.1.webmaster

Hi!

I fixed the code. This code snippet runs in a seperate thread:


PyObject *dict=NULL;
PyGILState_STATE state = PyGILState_Ensure();
dict = CreateMyGlobalDictionary();

PyRun_String(<my code>, Py_file_input, dict, dict);

ReleaseGlobalDictionary(dict);

But it still does not work... :-/
 
J

Jaimy Azle

I fixed the code. This code snippet runs in a seperate thread:


PyObject *dict=NULL;
PyGILState_STATE state = PyGILState_Ensure();
dict = CreateMyGlobalDictionary();

PyRun_String(<my code>, Py_file_input, dict, dict);

ReleaseGlobalDictionary(dict);

But it still does not work... :-/

Have you initialize interpreter with PyEval_InitThreads? look at
http://www.python.org/doc/1.5.2/api/threads.html for more information.

Oh, btw... I did use python in a bit different scenario than you've
described. Since you attempt to run different script per-host thread, you
might need python multiple interpreter support, I suggest you take a look
mod_python implementation.

Salam,

-Jaimy.
 
G

Graham Dumpleton

Have you initialize interpreter with PyEval_InitThreads? look athttp://www.python.org/doc/1.5.2/api/threads.htmlfor more information.

Oh, btw... I did use python in a bit different scenario than you've
described. Since you attempt to run different script per-host thread, you
might need python multiple interpreter support, I suggest you take a lookmod_pythonimplementation.

Please don't look at mod_python. Current versions of mod_python don't
use Python simplified GIL APIs correctly.

http://issues.apache.org/jira/browse/MODPYTHON-217

Look at mod_wsgi instead, it is closer to the mark, although still has
some not quite cruft in there to workaround mistakes in mod_python for
case where mod_python and mod_wsgi are being loaded together. :-(

Graham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top