Panda Threading

Hi,
i’m trying to develop a program that execute a Panda window as a child thread. See the code below:

from direct.stdpy import threading2 as threading
#import threading
from panda_shaft import shaft_drawer

class ab_thread(threading.Thread):

	def __init__(self): 
		self.ab_PandaModel = ab_PandaModel()
		threading.Thread.__init__(self)		
		
	def run(self):
		print "Starting Panda"
		while True:
			self.ab_PandaModel.taskMgr.step()
		
if __name__ == "__main__":

	app = ab_thread()
	app.start()
	raw_input("Press ENTER to continue")

but when I run this script the Panda window freeze. Can anyone help me? Thanks,
Ale

Panda’s Task Manager is not meant to be run in a child thread. It integrates closely with the threading system, and it will surely break if you call TaskMgr.step() in a child thread.

But it shouldn’t be necessary to do that; instead, you can use task chains to make the individual tasks run in a child thread. But note that if you wish to run the igloop task–the task that actually draws the frame–in a child thread, you might have similar problems; the rendering loop is not really designed for this either.

Is there a particular problem you are trying to solve that requires running the whole system in a child thread? If so, perhaps running in a child process (for instance, via the subprocess module) would be more effective.

David

Thank you for your quickly reply.
The problem I’d like to solve is simple. When I close the panda window the script that run it is terminate. See the following example:

data = ab_parse_file()
ab_panda_app = panda_app(data)
ab_panda_app.run()

#a lot of code follow

print "program complete"

So, while the panda window is open the script is not running, and when I close the panda window the script is terminated. I never reach the last line of code. I cannot move the line

ab_panda_app.run()

at the end of the script because I want to see an image of my model before continue the code.
I hope you can help me.

If you only want to have code execute when the program exits, you can override the method ShowBase.userExit in your own ShowBase class:

    def userExit(self):
        print "program complete"
        ShowBase.userExit(self)

David

I’ve already try to overload the method, but it seems that is not working. See the following code:

ab_class(ShowBase):
def userExit(self):
print "Program complete"
#ShowBase.userExit(self)

def __init__(self):
ab_model = loader.loadModel("model.egg")

app = ab_class()
app.run()

#a lot of code

print "continue"

Any suggestion?

What problem are you having? You have some syntax errors in the code you have pasted. If that is indeed the code, it’s not going to run as-is. Maybe you meant something more like this:

from direct.showbase.ShowBase import ShowBase

class ab_class(ShowBase):
    def userExit(self):
        print "Program complete"
        ShowBase.userExit(self)

    def __init__(self):
        ShowBase.__init__(self)
        ab_model = loader.loadModel("model.egg")

app = ab_class()
app.run()

But nothing will run after you call app.run(). It’s not supposed to. If you want stuff to run at the time the user closes the window, you put that stuff in the userExit() method. If you want stuff to run after the window is open, you put that stuff in a task so it will be called automatically, or you hang it on an accept() hook so it will be called when the user presses a key or something.

Look carefully at all of the sample programs. They all demonstrate code that is spawned at different times.

David