473,411 Members | 2,030 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,411 software developers and data experts.

Looking for advice: supporting multiple embedded interpreters

Some background first - we have some software that embeds a Python
interpreter into a host application. Scripts are loaded dynamically and
used. But we want to support the ability to edit scripts while the app is
running. This means we have to "unload" the script and cause a reload.
Normal module reloading tricks don't work because if you try to reimport a
script that imports another script, and if the nested script is changed, it
might not be reloaded itself. Also, we have 2 parts of the software, each
using Python for its own set of scripts. We don't want scripts loaded into
each parts to "see" scripts loaded into the other part. Ideally, we would
like multiple "instances" of a Python interpreter to manage this.

So, for Python 2.2, I came up with a system that works. When we need a new
self-contained Python interpreter, I use Py_NewInterpreter(), swap it in
using PyThreadState_Swap, load my built in modules, and swap it back out.
When I need to run some code in that interpreter, I swap it back in, load
the module I need, call methods in it, and swap it back out. When I'm done
with the interpreter, I swap it back in and call Py_EndInterpreter.

When I want to force a reload of all the script code in a given
interpreter, I just delete the interpreter, create a new one, and load the
scripts into that one. This has worked flawlessly. And each "part" of my
application can use a different interpreter without modules and globals in
each one interfering with the other.

Now, with Python 2.3, this code doesn't seem to work anymore. Someone told
me it is likely because of the extensive rewrite of GUSI or whatnot. It is
important to note that I'm NOT really doing any threading. I just was
self-contained interpreters for the above reasons.

What I am wondering is if there a reliable method in 2.3 that does what I
need?

It has recently come to my attention that Lutz Paelike is in exactly the
same situation I am in, so I don't think this is a fringe concept.

Jul 18 '05 #1
4 1803
> Some background first - we have some software that embeds a Python
interpreter into a host application. Scripts are loaded dynamically and
used. But we want to support the ability to edit scripts while the app is
running. This means we have to "unload" the script and cause a reload.
Normal module reloading tricks don't work because if you try to reimport a
script that imports another script, and if the nested script is changed, it
might not be reloaded itself. Also, we have 2 parts of the software, each
using Python for its own set of scripts. We don't want scripts loaded into
each parts to "see" scripts loaded into the other part. Ideally, we would
like multiple "instances" of a Python interpreter to manage this.

So, for Python 2.2, I came up with a system that works. When we need a new
self-contained Python interpreter, I use Py_NewInterpreter(), swap it in
using PyThreadState_Swap, load my built in modules, and swap it back out.
When I need to run some code in that interpreter, I swap it back in, load
the module I need, call methods in it, and swap it back out. When I'm done
with the interpreter, I swap it back in and call Py_EndInterpreter.

When I want to force a reload of all the script code in a given
interpreter, I just delete the interpreter, create a new one, and load the
scripts into that one. This has worked flawlessly. And each "part" of my
application can use a different interpreter without modules and globals in
each one interfering with the other.

Now, with Python 2.3, this code doesn't seem to work anymore. Someone told
me it is likely because of the extensive rewrite of GUSI or whatnot. It is
important to note that I'm NOT really doing any threading. I just was
self-contained interpreters for the above reasons.

What I am wondering is if there a reliable method in 2.3 that does what I
need?

It has recently come to my attention that Lutz Paelike is in exactly the
same situation I am in, so I don't think this is a fringe concept.


My advice: use the 'reload' builtin to reload your python modules.

- Josiah

Jul 18 '05 #2
Paul Miller <pa**@fxtech.com> writes:
What I am wondering is if there a reliable method in 2.3 that does
what I need?

It has recently come to my attention that Lutz Paelike is in exactly
the same situation I am in, so I don't think this is a fringe concept.


I can't address why it doesn't work in 2.3, but just a question - have
you thought of not using independent interpreter states, but just
tracking the contents of sys.modules and clearing out any new modules
at reload time? That would force even nested imports to be reloaded.
You can find an example of doing this in the unittestgui.py module,
for example, that is part of PyUnit, as it uses this approach to
ensure that all modules under test are reloaded at the start of
execution of a test suite.

I expect that you might be able to get even fancier by installing a
custom import hook, but just flushing sys.modules is pretty simple and
should work in any Python release.

Of course, it won't deal with stray references you may still have
around to the old module or module objects, but since the new
interpreter approach definitely can't be exhibiting that behavior
anyway, I expect you aren't using any older objects after such a
reload.

-- David
Jul 18 '05 #3
I can't address why it doesn't work in 2.3, but just a question - have
you thought of not using independent interpreter states, but just
tracking the contents of sys.modules and clearing out any new modules
at reload time? That would force even nested imports to be reloaded.


Hmm - although not as "clean" as having multiple interpreters (since
globals would still be visible between modules), it WOULD get around the
more pervasive problem, which was the reloading. I'll take a look at this.

THANKS!

Jul 18 '05 #4
I can't address why it doesn't work in 2.3, but just a question - have
you thought of not using independent interpreter states, but just
tracking the contents of sys.modules and clearing out any new modules
at reload time? That would force even nested imports to be reloaded.
You can find an example of doing this in the unittestgui.py module,
for example, that is part of PyUnit, as it uses this approach to
ensure that all modules under test are reloaded at the start of
execution of a test suite.


At first I thought this would work, but then I remembered that I have other
instances of interpreters and pointers to modules, objects, and functions
pointing into them. One of my issues is I do not want to affect other
loaded modules, and if I blow away the global module table, then I would
mess up all the other instances of scripts that I want to leave alone.


Jul 18 '05 #5

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

Similar topics

2
by: bmatt | last post by:
I am trying to support multiple interpreter instances within a single main application thread. The reason I would like separate interpreters is because objects in my system can be extended with...
7
by: rdh | last post by:
Hi all, I am in process of developing a Server in C++ supporting multiple protocols. The server will be exposing various functionalities, and the clients can communicate over any of the...
4
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application running on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
0
by: gabriel.becedillas | last post by:
Hi, At the company I work for we've embedded Python 2.4.1 in a C++ application. We execute multiple scripts concurrenlty, each one in its own interpreter (created using Py_NewInterpreter()). We...
3
by: BG Mahesh | last post by:
hi We are looking for a good Ajax library which has very good support for iframe. The ones we have considered so far are, Backbase.com - not happy with the speed Zapatech.com - it is good but...
0
by: vishnu | last post by:
Hello All, I have embedded python 2.5 in to my C application. As we need the python scripts to run in multi threaded environment I have used Py_NewInterpreter() and Py_EndInterpreter each time I...
12
by: amogan | last post by:
**If interested & qualified, please reply with your resume directly to amogan@google.com** Referrals are always welcome!! Network System Test Engineer - Mountain View This position is...
3
by: Marcin Kalicinski | last post by:
How do I use multiple Python interpreters within the same process? I know there's a function Py_NewInterpreter. However, how do I use functions like Py_RunString etc. with it? They don't take any...
7
by: skip | last post by:
This question was posed to me today. Given a C/C++ program we can clearly embed a Python interpreter in it. Is it possible to fire up multiple interpreters in multiple threads? For example: ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.