Thread / setupTaskChain


I wish to use thread in my game, and I wish to know the best pratices about.
I saw, we can use threading from panda, and I saw too the setupTaskChain.

By now, I was using task for everything, from default takschain, but I read that the taks are waiting the end of each task.

I wish to have in separate thread, different “task” to manage : AI, ODE physics, rendering, network…

I wish to know the best way to do, and how to share object between threads?

Thank you

Best way to use threads: use tasks instead. There’s a reason that tasks are recommended for Panda: they’re ten times easier to code, and a hundred times easier to debug.

Threads are provided for cases when you actually do need to have things running in parallel, for instance when you want to load models in the background or something (and even then, the best way to do this is usually with the callback parameter to loadModel()).

Putting your AI, physics, rendering, and networking functions in separate threads will not improve performance. In fact, it could hurt performance. And it will make your program much more complicated and much more likely to break.

There are times when it really is appropriate to use threads. But don’t use them until you encounter one of those times. :slight_smile:


Do I have to use default taskmanager, or create different one by using setupTaskChain?

What are the best practices?

For now, I have the meaning that waiting task to task may be too long? no?

Or this is a way to do asynchronous task?

If you insist on using threads, you should know:

(1) Threads available in Panda in the default build aren’t true threads, they are shared cooperatively on a single CPU. This means that you won’t get two things running in parallel anyway, except for certain things like disk I/O. If you want to use true threads, you will need to get Panda from source and build it yourself with the appropriate options.

(2) Even if you do this, Python itself doesn’t support true threading, so that all of your Python code must run on a single CPU. So you have to not only build Panda from source, but you have to write your program in C++.

(3) Once again, there’s usually not any reason to do this anyway. It won’t make your program faster, it will just make it broken.

(4) All that said, if you want to use threads as provided in the default build, you can. They work. You should use the default task manager, and just call taskMgr.setupTaskChain() to create a different task chain for each thread that you want. And then use the taskChain parameter for taskMgr.add() to assign your tasks to the different threaded task chains.

(5) Since Panda tasks are cooperative, you have to either make sure your tasks do not run for too much at a time (the same as if you’re not using threads), or you have to insert calls to Thread.considerYield() from time to time, for instance in the middle of a loop. This call will allow the thread to switch to a new context.


OK, I think I will keep default task manager, and follow your advices.

thanks for it.

Not specially upon my purpose, but I know that it is used in Eve online for example, what’s your opinion about stackless ?

And is it appyable to Panda?

I haven’t experimented with Stackless Python. I’ve heard promising things about it.

It doesn’t work with Panda today. It would no doubt be possible to adapt Panda to work with Stackless Python, but no one has yet volunteered to do that work.