Python Embedding Thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • otr2@tuningchannel.de

    Python Embedding Thread

    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_Rele ase and PyGILState_Ensu re 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_Import Module("__main_ _");
    if (!mod)
    return;

    f_globals = PyModule_GetDic t(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_loc als);
    Py_DECREF(f_loc als);






    Thanks a lot for your help :)


  • googler.1.webmaster@spamgourmet.com

    #2
    Re: Python Embedding Thread

    Does anyone has a tip?

    Comment

    • Benjamin

      #3
      Re: Python Embedding Thread

      On Jul 21, 6:56 am, o...@tuningchan nel.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_Rele ase and PyGILState_Ensu re 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_TH READS 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_Import Module("__main_ _");
                                      if (!mod)
                                              return;
      >
                                      f_globals= PyModule_GetDic t(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_loc als);
                                      Py_DECREF(f_loc als);
      >
      Thanks a lot for your help :)

      Comment

      • Jaimy Azle

        #4
        Re: Python Embedding Thread

        "Benjamin" <musiccompositi on@gmail.comwro te:
        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_TH READS 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





        Comment

        • googler.1.webmaster@spamgourmet.com

          #5
          Re: Python Embedding Thread

          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 :)

          Comment

          • googler.1.webmaster@spamgourmet.com

            #6
            Re: Python Embedding Thread

            Hi!

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


            PyObject *dict=NULL;
            PyGILState_STAT E state = PyGILState_Ensu re();
            dict = CreateMyGlobalD ictionary();

            PyRun_String(<m y code>, Py_file_input, dict, dict);

            ReleaseGlobalDi ctionary(dict);

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

            Comment

            • Jaimy Azle

              #7
              Re: Python Embedding Thread

              <googler.1.webm aster@spamgourm et.comwrote:
              I fixed the code. This code snippet runs in a seperate thread:
              >
              >
              PyObject *dict=NULL;
              PyGILState_STAT E state = PyGILState_Ensu re();
              dict = CreateMyGlobalD ictionary();
              >
              PyRun_String(<m y code>, Py_file_input, dict, dict);
              >
              ReleaseGlobalDi ctionary(dict);
              >
              But it still does not work... :-/
              Have you initialize interpreter with PyEval_InitThre ads? 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.


              Comment

              • Graham Dumpleton

                #8
                Re: Python Embedding Thread

                On Jul 23, 12:15 pm, "Jaimy Azle" <ja...@nospam.l og.web.idwrote:
                <googler.1.webm as...@spamgourm et.comwrote:
                I fixed the code. This code snippet runs in a seperate thread:
                >
                PyObject *dict=NULL;
                PyGILState_STAT E state = PyGILState_Ensu re();
                dict = CreateMyGlobalD ictionary();
                >
                PyRun_String(<m y code>, Py_file_input, dict, dict);
                >
                ReleaseGlobalDi ctionary(dict);
                >
                But it still does not work... :-/
                >
                Have you initialize interpreter with PyEval_InitThre ads? 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_pythoni mplementation.
                Please don't look at mod_python. Current versions of mod_python don't
                use Python simplified GIL APIs correctly.



                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

                Comment

                Working...