In my program, I have a sub thread that takes a long time to process data. My problem is that the main thread waits until the sub thread is done before it updates the frame, resulting in a frame rate of 0.1.
Is there any way to allow the main thread to update multiple frames before the sub thread finishes? Simplified code:
from direct.stdpy import threading
self.thread = threading.Thread(target=self.foobar,args=("foobar",))
#Waste time forever.
current_number = 1234567890
current_number /= 42
current_number += 2345734
self.server = Server()
You must be using Panda3D version 1.7.2, which used cooperative tasking to implement threads. In this version, you must call Thread.considerYield() inside your thread’s while loop to allow the main thread to run from time to time.
In Panda3D version 1.8.0 and above, this isn’t necessary because it is compiled to use true system threads.
Okay, I switched to version 1.8.1, and now when I try to use threading, on the second thread I create, the program stops responding, and I have to force quit it. (the function doesn’t really loop forever, so I don’t think it is waiting for it anymore.) Also, upon setting the thread to daemon, I get the following error:
Traceback (most recent call last):
File "/Users/jlawrence/dev/SuperStructure/online.py", line 63, in readpol
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py", line 80, in __setattr__
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py", line 69, in setDaemon
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py", line 60, in is_alive
AttributeError: Thread instance has no attribute '_ThreadBase__thread'
This didn’t happen in version 1.7. In case it is relevant, I am trying to create procedural geometry in the sub thread. When i take out the procedural geometry generation, it works fine, but my frame rate drops to 1.3, even though the thread is not doing anything and should have ended. Also, if i take out the threading altogether, it takes a while to process but the frame rate returns to normal.
There is a reported bug with creating geometry in a sub-thread; it appears to lock up the program, as you have discovered. We are investigating.
Panda threads don’t support setting daemon, but you can import the system threading module instead of direct.stdpy.threading, which is safe to do in 1.8. Of course there’s still the deadlock issue in 1.8.
Thanks for the feedback! I can wait for the next version of panda3d to implement threading, and just work around the low framerate for now.