1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
"""Task.py
Functionality for running multiple tasks simultaneously.
Classes:
Task Processing that can be forked off in a separate process.
"""
# Copied from threading.py
# This is not thread safe!
_counter = 0
def _newname(template="Task-%d"):
"""_newname(template="Task-%d") -> name"""
global _counter
_counter = _counter + 1
return template % _counter
class Task:
"""Contains information for one process.
Implements part of the Thread interface.
Methods:
start Start this task. Should be called once.
run Called by start to really run the task.
getName Get the name of the task.
setName Set the name of the task.
isAlive Whether this Task is still running.
Members:
retval Return value of the function.
"""
def __init__(self, group=None, target=None, name=None,
args=(), kwargs={}):
"""Task([group][, target][, name][, args][, kwargs])
Create a task object. group should be None and is reserved
for future expansion. target is the function to be called.
name is the name of the thread. args and kwargs are the
arguments to be passed to target.
"""
self._target = target
if name is None:
name = _newname()
self._name = str(name)
self._args, self._kwargs = args, kwargs
self._start = self._finished = None
self._handle = None
self.retval = None
def __del__(self):
# Close my handle if it's not finished running.
if self._handle:
self._handle.close()
def start(self):
"""S.start()
Start this task. Should only be called once.
"""
if self._start is not None:
raise ValueError, "task %s already started" % self._name
self._start = 1
self.run()
def run(self):
"""S.run()
Run this task. Should only be called by S.start().
"""
import copen
# If the client didn't specify a target function, then don't
# do any processing.
if not self._target:
self._finished = 1
else:
self._handle = copen.copen_fn(
self._target, *self._args, **self._kwargs)
def getName(self):
"""S.getName() -> name"""
return self._name
def setName(self, name):
"""S.setName(name)"""
self._name = name
def isAlive(self):
"""S.isAlive() -> boolean"""
if not self._finished:
if self._handle and self._handle.poll():
self.retval = self._handle.read()
self._finished = 1
return not self._finished
|