Threads in embedded interpreter doesn't run

D

devrim.erdem

Hello,

I am trying to implement threads in my embedded interpreter.

The host application is a C++ app which has a built-in python
interpreter. After initialization of the app just before it starts an
endless while loop ( which does OpenGL rendering ) the thread is
created and started. Like this :

class TmpThread( threading.Thread ):
def __init__( self ):
threading.Thread.__init__( self )
print "%s is constructed." % self
def run(self):
while( 1 ):
print "this is tmpthread"
time.sleep( 1 )

tmp = TmpThread()
tmp.start()

The problem is that the thread prints its message a couple of times
and then stops. During the main loop of my program, the python thread
doesn't run. After termination of the loop, the thread starts printing
the message.

Could anyone give me some hints to understand why the thread can not
execute during my application loop.

Thanks in advance,
Devrim.
 
J

Jeff Shannon

Could anyone give me some hints to understand why the thread can not
execute during my application loop.

This is a complete shot in the dark, but is there any chance that your
rendering loop is holding on to the Python GIL? Doing so would prevent
any (other) Python code from running for as long as the lock is held.

Jeff Shannon
Technician/Programmer
Credit International
 
D

devrim.erdem

Jeff Shannon said:
This is a complete shot in the dark, but is there any chance that your
rendering loop is holding on to the Python GIL? Doing so would prevent
any (other) Python code from running for as long as the lock is held.

Thanks Jeff.

I haven't so far explicitly done anything with Python GIL in my
application. Is there a function which could tell me if the Python GIL
is held or not ?
 
J

Jeff Shannon

Thanks Jeff.

I haven't so far explicitly done anything with Python GIL in my
application. Is there a function which could tell me if the Python GIL
is held or not ?

I've never done any extending/embedding, so I don't really know; I'm
just working with what I've seen mentioned here on c.l.py (and I usually
skip extending/embedding threads, since I don't do it). But I believe
that the GIL is released and reclaimed by (from vague memory)
PyBeginAllowThreads and PyEndAllowThreads, or something like that.
You'll need to read through the extending/embedding docs to figure out
how they work, though, because I don't know enough to help you there.
:) Just remember that you can't touch any Python internals (or do much
of anything through the interpreter) unless you hold the GIL, and that
no other Python threads can run while you hold it.

Jeff Shannon
Technician/Programmer
Credit International
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top