I’ve written a data visualizer that pulls fresh data off the internet every 5 minutes and displays this data within the 3D Panda environment.
Initially, I had the update code running as a doMethodLater task that just repeats every 5 minutes. Since the visualization is always moving (think animated tag cloud) you would see the graphics freeze when the update method was running. Although the internet request is usually complete is less than a second, I want to get rid of the graphics freezing when updating the data. In this update method I’m not doing anything Panda specific (no Panda library calls). I’m simply building a list of the data returned from an XML request.
In the future I also will want to move this closer to real-time data either through the use of long-polling or pushing data to the application. I’ve read all the documentation as well as any forum posts talking about threading, and I’m thinking this will be the way to go. I’ve tried something similar to what you see below, but it still seems that the graphics freeze during the update. I would appreciate any ideas on how I could implement this.
I’ve really simplified the code here to show my method of implementing threaded updates. This code seems to be leaking memory (possibly creating infinite new threads?) as well as not fixing the graphics freezing issue.
<Other Panda Imports>
from direct.stdpy import threading2 as threading
class UpdateThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.myData = []
def run(self):
#Here I make an internet request and process the returned data
class MyVisualization(DirectObject):
def __init__(self):
self.updateObj = None
self.myData = []
#Repeating task (every 5 seconds) to kick off update thread
self.updateTask = taskMgr.doMethodLater(5, self.update, 'update task')
def update(self, task):
if self.updateObj is not None:
if not self.updateObj.isAlive():
self.updateObj = UpdateThread()
self.updateObj.start()
else:
self.updateObj = UpdateThread()
self.updateObj.start()
return task.again