By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,361 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,361 IT Pros & Developers. It's quick & easy.

Python Embedding Thread

P: n/a
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 :)
Jul 21 '08 #1
Share this Question
Share on Google+
7 Replies

P: n/a
Does anyone has a tip?
Jul 21 '08 #2

P: n/a
On Jul 21, 6:56*am, o...@tuningchannel.de wrote:
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.
>
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 :)
Jul 22 '08 #3

P: n/a
"Benjamin" <mu**************@gmail.comwrote:
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

Jul 22 '08 #4

P: n/a
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 :)
Jul 22 '08 #5

P: n/a
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... :-/
Jul 22 '08 #6

P: n/a
<go*****************@spamgourmet.comwrote:
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.
Jul 23 '08 #7

P: n/a
On Jul 23, 12:15*pm, "Jaimy Azle" <ja...@nospam.log.web.idwrote:
<googler.1.webmas...@spamgourmet.comwrote:
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 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

Jul 23 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.