Multiple running parsers, different threads, embedded in C++..?

eparker at mail.kickingdragon.com eparker at mail.kickingdragon.com
Fri Nov 12 02:44:50 CET 2004


Hello!

I'm new to this list, so please be gentle. :)

I've been scouring the net for an answer to this, as well as trying to get
a little demo running, but to no avail, so I'm going to plead some help
from the court, about a multithreading C++, which embeds Python parsers..
Hopefully I'm dumb, and it's an easy answer. :)

So, here goes:

I'm trying to embed a Python parser into C++, and so far, it works
beautifully (I thought there could be nothing sexier then Python, and then
Boost.Python came along too. I could cry, I'm so happy). 

Now, I want to embed this into an application that is serving web pages
(required to be standalone server), however I keep reading about the GIL
and how Python is not reentrant. However, it's locking scenario sounds like
it try's to make the best of it, and it sounds like other applications make
their way around it just fine.. even mod_python (or is that forking,
happening?).

I'm curious if this restriction will have me wallowing in pain, as it might
severely slow down the server? Does anyone know if this will be a huge
issue? (I'm grasping at straws here: Python & Boost.Python does EVERYTHING
else I need/want. :))

I'm also trying to do a demo app, to test the speed, but I'm actually
having problem GETTING threading going.. Perhaps it's my lack of knowledge
with the system, but I was wondering if I could solicit some advice?

I'm getting errors like the following:

Fatal Python error: PyThreadState_Get: no current thread
Fatal Python error: PyThreadState_Get: no current thread

And as for what my code looks like (as of now, anyways: it's been through
more surgery then Michael Jackson, today), it's at the bottom of this
message.

Is there anything I'm doing horribly wrong? I don't claim to be an expert
on threading, but YOU could change this. :) 

Thanks so much.. Please don't break my heart and tell me I can't use
Python. :)

-e-


unsigned __stdcall ExecutePython(void *) 
{
    const char *kFileName = "TestFile.py";

    PyObject *pPyFileObject = PyFile_FromString(const_cast<char
*>(kFileName), "r");

    PyRun_SimpleFile(PyFile_AsFile(pPyFileObject), kFileName);

    Py_DECREF(pPyFileObject);

    return 0;
}   

int main(void)
{
   Py_Initialize();
   PyEval_InitThreads();

   const unsigned int kNumThreads = 2;

   HANDLE hThreads[kNumThreads];
   unsigned int threadIDs[kNumThreads];

   PyThreadState *tstate = PyEval_SaveThread();
   
   for(unsigned int i = 0;
       i < kNumThreads;
       ++i)
   {
        PyEval_RestoreThread(tstate);       
        hThreads[i] = (HANDLE)(_beginthreadex(NULL, 0, &ExecutePython,
NULL, 0, &threadIDs[i]));
        tstate = PyEval_SaveThread();
   }

   
   for(unsigned int i = 0;
       i < kNumThreads;
       ++i)
   {
       cout << "Waiting for thread #" << i << endl;
       WaitForSingleObject(hThreads[i], INFINITE );
       CloseHandle(hThreads[i]);
   }

   PyEval_RestoreThread(tstate);
   Py_Finalize();
   // Python interpreter has ended.

   return 0;
}

--------------------------------------------------------------------
mail2web - Check your email from the web at
http://mail2web.com/ .





More information about the Python-list mailing list