1 """Task.py
2
3 Functionality for running multiple tasks simultaneously.
4
5 Classes:
6 Task Processing that can be forked off in a separate process.
7
8 """
9
10 import warnings
11 warnings.warn("Bio.MultiProc is deprecated. If you want to use this code, please let the Biopython developers know by sending an email to biopython-dev@biopython.org to avoid permanent removal of Bio.MultiProc.",
12 DeprecationWarning)
13
14
15
16 _counter = 0
22
24 """Contains information for one process.
25
26 Implements part of the Thread interface.
27
28 Methods:
29 start Start this task. Should be called once.
30 run Called by start to really run the task.
31 getName Get the name of the task.
32 setName Set the name of the task.
33 isAlive Whether this Task is still running.
34
35 Members:
36 retval Return value of the function.
37
38 """
39 - def __init__(self, group=None, target=None, name=None,
40 args=(), kwargs={}):
41 """Task([group][, target][, name][, args][, kwargs])
42
43 Create a task object. group should be None and is reserved
44 for future expansion. target is the function to be called.
45 name is the name of the thread. args and kwargs are the
46 arguments to be passed to target.
47
48 """
49 self._target = target
50 if name is None:
51 name = _newname()
52 self._name = str(name)
53 self._args, self._kwargs = args, kwargs
54 self._start = self._finished = None
55 self._handle = None
56 self.retval = None
57
59
60 if self._handle:
61 self._handle.close()
62
64 """S.start()
65
66 Start this task. Should only be called once.
67
68 """
69 if self._start is not None:
70 raise ValueError, "task %s already started" % self._name
71 self._start = 1
72 self.run()
73
75 """S.run()
76
77 Run this task. Should only be called by S.start().
78
79 """
80 import copen
81
82
83 if not self._target:
84 self._finished = 1
85 else:
86 self._handle = copen.copen_fn(
87 self._target, *self._args, **self._kwargs)
88
90 """S.getName() -> name"""
91 return self._name
92
94 """S.setName(name)"""
95 self._name = name
96
98 """S.isAlive() -> boolean"""
99 if not self._finished:
100 if self._handle and self._handle.poll():
101 self.retval = self._handle.read()
102 self._finished = 1
103 return not self._finished
104