Embedding, subinterpreters and dynamic types

Discussion in 'Python' started by Mikael Liljeroth, Aug 13, 2010.

  1. Hi, this is my first contact with the python community so I hope this
    is the right place for my two questions.
    They are both related to embedding and extending python.

    I'm writing a program that dynamically loads c++ modules that extend
    the functionality of my program.
    These modules are controlled by scripts. Currently I have embedded the
    v8 javascript engine but I would also like to support scripts written
    in python. I have a special interface between the c++ modules and the
    scripts to be able to support different scripting languages. Scripts
    are separated into different 'applications' that are executed in one
    process triggered by external events (no threading, since I'm working
    in an event driven system). The scripts should execute independently
    of each other. I solved this with v8 by switching between the
    applications' contexts. However, it seems like python has some issues
    with this.

    1. To do some basic context switching I assume I should use a sub
    interpreter ie create a new thread state (without any actual threads).
    I cannot find any information on how to do this except creating the
    new thread state. Is this the preferred way of doing this, if so, how
    do I switch between different thread states and execute scripts in
    them? I assume scripts running in one thread state are isolated from
    scripts running in other thread states, like global variables and such
    (except for the GIL thing?) ? I do not care much about security
    problems like file descriptor issues and extension issues right now.

    2. When a new C++ module is loaded into my program I want to create a
    representation of it in Python. Not as a type, but rather just return
    a single object instance reference of a new type representing the
    exported attributes and methods of the module's C++ class. In other
    words, I want to be able to do something like this obj =
    application.getObject("objectId"); where objectId is the name of an
    instance of that c++ module type. I do not want the script to be able
    to create new instances of this type, since this is done via xml in my
    program. Hence I would also like to be able to associate my C++ object
    with my returned python object to be able to manipulate it when
    methods are called on 'obj'. So how do I define a new type on the fly
    and create an instance of it, associated with a c++ pointer and return
    it in the c function representing the python method 'getObject' in my
    application module?

    Hope this makes any sense, let me know if something is unclear.

    Mikael Liljeroth
    Mikael Liljeroth, Aug 13, 2010
    1. Advertisements

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. cr88192
    Apr 28, 2009
  2. MikeL
    Dino Chiesa [Microsoft]
    Feb 22, 2005
  3. Chris Nafziger
    Alex Deveza
    Oct 17, 2004
  4. ittium
    Dec 9, 2011
  5. Sam Kong
    John G Harris
    Oct 17, 2006

Share This Page