new task API with extraArgs

This commit is contained in:
Joe Shochet 2003-10-12 06:14:56 +00:00
parent 1b515b56ee
commit fe6d1f085a
2 changed files with 28 additions and 34 deletions

View File

@ -15,20 +15,17 @@
""" """
if not name: if not name:
name = self.getUrl().cStr() name = self.getUrl().cStr()
import Task import Task
task = Task.Task(self.doTask) task = Task.Task(self.doTask)
task.callback = callback task.callback = callback
task.extraArgs = extraArgs task.callbackArgs = extraArgs
return taskMgr.add(task, name)
return taskMgr.add(task, name)
def doTask(self, task): def doTask(self, task):
import Task import Task
if self.run(): if self.run():
return Task.cont return Task.cont
if task.callback: if task.callback:
task.callback(*task.extraArgs) task.callback(*task.callbackArgs)
return Task.done return Task.done

View File

@ -71,6 +71,7 @@ class Task:
self.pstats = None self.pstats = None
self.__removed = 0 self.__removed = 0
self.__onDoLaterList = 0 self.__onDoLaterList = 0
self.extraArgs = None
def setOnDoLaterList(self, status): def setOnDoLaterList(self, status):
self.__onDoLaterList = status self.__onDoLaterList = status
@ -121,7 +122,6 @@ class Task:
else: else:
return ('Task id: %s, no name' % (self.id)) return ('Task id: %s, no name' % (self.id))
def pause(delayTime): def pause(delayTime):
def func(self): def func(self):
if (self.time < self.delayTime): if (self.time < self.delayTime):
@ -407,7 +407,14 @@ class TaskManager:
continue continue
return cont return cont
def __spawnDoLater(self, task): def doMethodLater(self, delayTime, func, taskName, extraArgs=None, uponDeath=None):
task = Task(func)
task.delayTime = delayTime
task.name = taskName
if extraArgs:
task.extraArgs = extraArgs
if uponDeath:
task.uponDeath = uponDeath
if TaskManager.notify.getDebug(): if TaskManager.notify.getDebug():
TaskManager.notify.debug('spawning doLater: %s' % (task)) TaskManager.notify.debug('spawning doLater: %s' % (task))
# Add this task to the nameDict # Add this task to the nameDict
@ -429,18 +436,7 @@ class TaskManager:
sentArgs = [task, task.name, task.id]) sentArgs = [task, task.name, task.id])
return task return task
def doLater(self, delayTime, task, taskName): def add(self, funcOrTask, name, priority=0, extraArgs=None, uponDeath=None):
if TaskManager.notify.getDebug():
TaskManager.notify.debug('doLater: %s' % (taskName))
task.delayTime = delayTime
task.name = taskName
return self.__spawnDoLater(task)
def doMethodLater(self, delayTime, func, taskName):
task = Task(func)
return self.doLater(delayTime, task, taskName)
def add(self, funcOrTask, name, priority = 0):
""" """
Add a new task to the taskMgr. Add a new task to the taskMgr.
You can add a Task object or a method that takes one argument. You can add a Task object or a method that takes one argument.
@ -448,22 +444,17 @@ class TaskManager:
if TaskManager.notify.getDebug(): if TaskManager.notify.getDebug():
TaskManager.notify.debug('add: %s' % (name)) TaskManager.notify.debug('add: %s' % (name))
if isinstance(funcOrTask, Task): if isinstance(funcOrTask, Task):
funcOrTask.setPriority(priority) task = funcOrTask
return self.__spawnTaskNamed(funcOrTask, name)
elif callable(funcOrTask): elif callable(funcOrTask):
return self.__spawnMethodNamed(funcOrTask, name, priority) task = Task(funcOrTask, priority)
else: else:
self.notify.error('add: Tried to add a task that was not a Task or a func') self.notify.error('add: Tried to add a task that was not a Task or a func')
task.setPriority(priority)
def __spawnMethodNamed(self, func, name, priority=0):
task = Task(func, priority)
return self.__spawnTaskNamed(task, name)
def __spawnTaskNamed(self, task, name):
if TaskManager.notify.getDebug():
TaskManager.notify.debug('__spawnTaskNamed: %s' % (name))
# Init params
task.name = name task.name = name
if extraArgs:
task.extraArgs = extraArgs
if uponDeath:
task.uponDeath = uponDeath
# be sure to ask the globalClock for the current frame time # be sure to ask the globalClock for the current frame time
# rather than use a cached value; globalClock's frame time may # rather than use a cached value; globalClock's frame time may
# have been synced since the start of this frame # have been synced since the start of this frame
@ -606,13 +597,19 @@ class TaskManager:
task.setCurrentTimeFrame(self.currentTime, self.currentFrame) task.setCurrentTimeFrame(self.currentTime, self.currentFrame)
if not self.taskTimerVerbose: if not self.taskTimerVerbose:
# don't record timing info # don't record timing info
ret = task(task) if task.extraArgs:
ret = apply(task, task.extraArgs)
else:
ret = task(task)
else: else:
# Run the task and check the return value # Run the task and check the return value
if task.pstats: if task.pstats:
task.pstats.start() task.pstats.start()
startTime = globalClock.getRealTime() startTime = globalClock.getRealTime()
ret = task(task) if task.extraArgs:
ret = apply(task, task.extraArgs)
else:
ret = task(task)
endTime = globalClock.getRealTime() endTime = globalClock.getRealTime()
if task.pstats: if task.pstats:
task.pstats.stop() task.pstats.stop()