473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

embedded python and interpreter threads

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
Jul 18 '05 #1
2 1860
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");

Jul 18 '05 #2
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");

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ugo Di Girolamo | last post by:
I have the following code, that seems to make sense to me. However, it crashes about 1/3 of the times. My platform is Python 2.4.1 on WXP (I tried the release version from the msi and...
1
by: amit | last post by:
Hello, I am embedding a python script in a C++ application. The script can be called simultaneously from multiple threads. What is the correct way to implement this situation: 1) Have...
0
by: Dirk Runge | last post by:
Hi! I have embedded Python in an C++ App. The Python-Interpreter is running in its own Thread (I'm using PThreads). I use PyRun_SimpleString to run Python-Code that the user entered in an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.