[Stackless] Python, C, context-switching

Joel Vennin joel.vennin at free.fr
Tue Sep 28 17:16:21 CEST 2004

       Hello Python community, 
i've got complex problem with python, c++
library and context-switching.

I introduce the problem:

  I've a C/C++ library which is an event driven simulator. My C/C++
  simulator use context-switching to simulate concurrency. It's using
  user-thread mechanism. My simulator use some object as Element that
  contains methods. This methods are called during the simulation.

  I've made wrapper of my simulator with swig. So now i've a PyElement
  class which is a wrapper of my Element C++ class.

  I'm now able to create a scenario using python script:


  class Car (PyElement):
        def __init__ (self):
            PyElement.__init__ (self)
            self.registerCallback (self.stop)

        def stop (self):
            while 1:
                 print "before stop"
                 self.nextEvent ()
                 print "after stop"            

This is a simple example: I create an PyElement that register a
Python callback inside my C++ simulator. So when i instanciate a
Python Car element, my C++ simulator are aware of the creation of an
object Element, which has a python callback method.

I've tested all basics(instanciate python class, callback mechanism
with C++/python, etc)  functionalities and all works fine.

The problem arrives when i'm using the context switching mechanism of
my simulator. How does it works ?
When the simulator starts, it create a context-switching for each
registered callback. And it switch the context to the first registered
callback to execute it. During the execution of the callback it can
have a nextEvent call. The nextEvent call function change the current
context to the next context and execute the callback associated to the
new context. You can see each callback function like a thread with a
wait method (here, it's called nextEvent).

 When all my system is design in C++, all work fine. But when i'm
 using python, the first switching context make a python segfault.
It's the trace when my python callback is called by my simulator just after a 
context switching.

#0  0x0810202c in PyFrame_New (tstate=0x0, code=0x402ea7a0, 
    globals=0x402aa41c, locals=0x0) at Objects/frameobject.c:569
#1  0x080aa708 in PyEval_EvalCodeEx (co=0x402ea7a0, globals=0x402aa41c, 
    locals=0x0, args=0x402f64f8, argcount=1, kws=0x0, kwcount=0, defs=0x0, 
    defcount=0, closure=0x0) at Python/ceval.c:2698
#2  0x081043a0 in function_call (func=0x402f0844, arg=0x402f64ec, kw=0x0)
    at Objects/funcobject.c:506
#3  0x0805cdd9 in PyObject_Call (func=0x402f0844, arg=0x402aa41c, 
    kw=0x402aa41c) at Objects/abstract.c:1757
#4  0x08063844 in instancemethod_call (func=0x402f0844, arg=0x402f64ec, 
    kw=0x402aa41c) at Objects/classobject.c:2454
#5  0x0805cdd9 in PyObject_Call (func=0x402f7bbc, arg=0x402aa41c, 
    kw=0x402aa41c) at Objects/abstract.c:1757
#6  0x080ab978 in PyEval_CallObjectWithKeywords (func=0x402aa41c, 
    arg=0x4029d02c, kw=0x402f7bbc) at Python/ceval.c:3619
#7  0x0805cd7e in PyObject_CallObject (o=0x402aa41c, a=0x402aa41c)
    at Objects/abstract.c:1747

I find no solution to solve this problem, can you help me and give me
some clues

Thanks you very much


Stackless mailing list
Stackless at stackless.com

More information about the Stackless mailing list