embedded python and interpreter threads

Discussion in 'Python' started by Charlie DeTar, Dec 7, 2004.

  1. So, I have an amazing, functioning foobar class that embeds python.
    Only trouble is, I want to be able to have multiple foobar objects at
    once, each with their own interpreters, stdouts, and stderrs.

    My initialization of the python interpreter in the class "x" is as follows:
    [snip]
    if (!Py_IsInitialized()) {
    PyEval_InitThreads();
    Py_Initialize();
    }

    // Start and switch to a new interpreter thread.
    x->thread = Py_NewInterpreter();

    // initialize the module 'logMethods', defined elsewhere,
    // which contains my stdout and stderr definitions
    Py_InitModule("log", logMethods);

    // overwrite Python's stdout and stderr
    PyRun_SimpleString(
    "import log\n"
    "import sys\n"
    "class StdoutCatcher:\n"
    "\tdef write(self, str):\n"
    "\t\t.CaptureStdout(str)\n"
    "class StderrCatcher:\n"
    "\tdef write(self, str):\n"
    "\t\t.CaptureStderr(str)\n"
    "sys.stdout = StdoutCatcher()\n"
    "sys.stderr = StderrCatcher()\n");

    PyEval_ReleaseThread(x->thread);
    [snip]

    Everything seems to work as expected - each object seems to have its own
    interpreter (if I evaluate "tree = 'a larch'" in one of them, 'tree' is
    not thusly defined in the others). Since I store the threadstate as a
    property of the object, I can easily juggle the interpreter lock between
    objects when evaluating python code. However, the last foobar object to
    be instantiated collects the stdout and stderr of all the other objects
    (as though the stdout and stderr definitions were shared over all the
    objects). This seems strange, as the docs say that if I start a
    "Py_NewInterpreter()" I get new copies of all the modules, particuarly
    'sys', and explicitly 'stdout' and 'stderr'.

    I thought of the possibility that the 'logMethods' structure which
    includes my versions of stdout and stderr might be behaving statically.
    But I didn't define it as static... I am new to C, so there might be
    something basic I am missing. Any suggestions or ideas?

    Thanks,
    Charlie DeTar
     
    Charlie DeTar, Dec 7, 2004
    #1
    1. Advertising

  2. Quick correction, sorry - the command should read like this (I had
    omitted the "log" before 'CaptureStdout' and 'CaptureStderr'). Same
    problems exist.

    PyRun_SimpleString(
    "import log\n"
    "import sys\n"
    "class StdoutCatcher:\n"
    "\tdef write(self, str):\n"
    "\t\tlog.CaptureStdout(str)\n"
    "class StderrCatcher:\n"
    "\tdef write(self, str):\n"
    "\t\tlog.CaptureStderr(str)\n"
    "sys.stdout = StdoutCatcher()\n"
    "sys.stderr = StderrCatcher()\n");

    =Charlie

    Charlie DeTar wrote:
    > So, I have an amazing, functioning foobar class that embeds python. Only
    > trouble is, I want to be able to have multiple foobar objects at once,
    > each with their own interpreters, stdouts, and stderrs.
    >
    > My initialization of the python interpreter in the class "x" is as follows:
    > [snip]
    > if (!Py_IsInitialized()) {
    > PyEval_InitThreads();
    > Py_Initialize();
    > }
    >
    > // Start and switch to a new interpreter thread.
    > x->thread = Py_NewInterpreter();
    >
    > // initialize the module 'logMethods', defined elsewhere,
    > // which contains my stdout and stderr definitions
    > Py_InitModule("log", logMethods);
    >
    > // overwrite Python's stdout and stderr
    > PyRun_SimpleString(
    > "import log\n"
    > "import sys\n"
    > "class StdoutCatcher:\n"
    > "\tdef write(self, str):\n"
    > "\t\t.CaptureStdout(str)\n"
    > "class StderrCatcher:\n"
    > "\tdef write(self, str):\n"
    > "\t\t.CaptureStderr(str)\n"
    > "sys.stdout = StdoutCatcher()\n"
    > "sys.stderr = StderrCatcher()\n");
     
    Charlie DeTar, Dec 7, 2004
    #2
    1. Advertising

  3. Woohoo, fixed my problem. It had to do with the way I was handling the
    methods which overwrote stderr and stdout. Yeehaw!

    =Charlie


    Charlie DeTar wrote:
    > Quick correction, sorry - the command should read like this (I had
    > omitted the "log" before 'CaptureStdout' and 'CaptureStderr'). Same
    > problems exist.
    >
    > PyRun_SimpleString(
    > "import log\n"
    > "import sys\n"
    > "class StdoutCatcher:\n"
    > "\tdef write(self, str):\n"
    > "\t\tlog.CaptureStdout(str)\n"
    > "class StderrCatcher:\n"
    > "\tdef write(self, str):\n"
    > "\t\tlog.CaptureStderr(str)\n"
    > "sys.stdout = StdoutCatcher()\n"
    > "sys.stderr = StderrCatcher()\n");
    >
    > =Charlie
    >
    > Charlie DeTar wrote:
    >
    >> So, I have an amazing, functioning foobar class that embeds python.
    >> Only trouble is, I want to be able to have multiple foobar objects at
    >> once, each with their own interpreters, stdouts, and stderrs.
    >>
    >> My initialization of the python interpreter in the class "x" is as
    >> follows:
    >> [snip]
    >> if (!Py_IsInitialized()) {
    >> PyEval_InitThreads();
    >> Py_Initialize();
    >> } // Start and switch to a new interpreter thread.
    >> x->thread = Py_NewInterpreter();
    >>
    >> // initialize the module 'logMethods', defined elsewhere,
    >> // which contains my stdout and stderr definitions
    >> Py_InitModule("log", logMethods);
    >> // overwrite Python's stdout and stderr
    >> PyRun_SimpleString(
    >> "import log\n"
    >> "import sys\n"
    >> "class StdoutCatcher:\n"
    >> "\tdef write(self, str):\n"
    >> "\t\t.CaptureStdout(str)\n"
    >> "class StderrCatcher:\n"
    >> "\tdef write(self, str):\n"
    >> "\t\t.CaptureStderr(str)\n"
    >> "sys.stdout = StdoutCatcher()\n"
    >> "sys.stderr = StderrCatcher()\n");
     
    Charlie DeTar, Dec 7, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    399
    Jeff Shannon
    Oct 21, 2004
  2. Peter Newman
    Replies:
    1
    Views:
    447
  3. Replies:
    4
    Views:
    603
  4. TK
    Replies:
    1
    Views:
    334
    Scott David Daniels
    Nov 30, 2005
  5. Replies:
    3
    Views:
    779
    Ziga Seilnacht
    Jan 3, 2007
Loading...

Share This Page