1 """
2 Simple scheduling of threads to be run.
3
4 Classes:
5 Scheduler Schedules threads to be run.
6
7 """
8
9 import warnings
10 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.",
11 DeprecationWarning)
12
13
15 """Schedules threads to be run. No prioritization. Nothing fancy.
16
17 Methods:
18 add Add a thread to be run.
19 num_left Return the number of threads left.
20 num_running Return the number of threads currently running.
21 run Main loop. Returns whether there's still threads left.
22
23 """
24 - def __init__(self, max_threads, start_fn=None, finish_fn=None):
25 """Scheduler(max_threads[, start_fn][, finish_fn]) -> object
26
27 max_threads is the maximum number of threads to run at a time.
28 start_fn and finish_fn are optional callbacks that take a
29 thread as an argument. They are called before and after each
30 thread.
31
32 """
33 if max_threads <= 0:
34 raise ValueError, "You must specify a positive number of threads!"
35 self._max_threads = max_threads
36 self._start_fn = start_fn
37 self._finish_fn = finish_fn
38 self._waiting = []
39 self._in_progress = []
40
42 """S.run() -> boolean
43
44 Execute the main loop. Return a boolean indicating whether
45 threads are still running.
46
47 """
48
49
50 i=0
51 while i < len(self._in_progress):
52 if not self._in_progress[i].isAlive():
53 t = self._in_progress.pop(i)
54 if self._finish_fn:
55 self._finish_fn(t)
56 else:
57 i = i + 1
58
59
60 while self._waiting and len(self._in_progress) < self._max_threads:
61 t = self._waiting.pop(0)
62 if self._start_fn:
63 self._start_fn(t)
64 t.start()
65 self._in_progress.append(t)
66
67 return self._waiting or self._in_progress
68
69 - def add(self, thread):
70 """S.add(thread)"""
71 self._waiting.append(thread)
72
74 """S.num_left() -> number of threads left to run"""
75 return len(self._waiting)
76
78 """S.num_running() -> number of threads currently running"""
79 return len(self._in_progress)
80