Class to manage a pool of backgrounded threaded jobs. Below, we assume that 'jobs' is a BackgroundJobManager instance. Usage summary (see the method docstrings for details): jobs.new(...) -> start a new job jobs() or jobs.status() -> print status summary of all jobs
| 39 | |
| 40 | |
| 41 | class BackgroundJobManager(object): |
| 42 | """Class to manage a pool of backgrounded threaded jobs. |
| 43 | |
| 44 | Below, we assume that 'jobs' is a BackgroundJobManager instance. |
| 45 | |
| 46 | Usage summary (see the method docstrings for details): |
| 47 | |
| 48 | jobs.new(...) -> start a new job |
| 49 | |
| 50 | jobs() or jobs.status() -> print status summary of all jobs |
| 51 | |
| 52 | jobs[N] -> returns job number N. |
| 53 | |
| 54 | foo = jobs[N].result -> assign to variable foo the result of job N |
| 55 | |
| 56 | jobs[N].traceback() -> print the traceback of dead job N |
| 57 | |
| 58 | jobs.remove(N) -> remove (finished) job N |
| 59 | |
| 60 | jobs.flush() -> remove all finished jobs |
| 61 | |
| 62 | As a convenience feature, BackgroundJobManager instances provide the |
| 63 | utility result and traceback methods which retrieve the corresponding |
| 64 | information from the jobs list: |
| 65 | |
| 66 | jobs.result(N) <--> jobs[N].result |
| 67 | jobs.traceback(N) <--> jobs[N].traceback() |
| 68 | |
| 69 | While this appears minor, it allows you to use tab completion |
| 70 | interactively on the job manager instance. |
| 71 | """ |
| 72 | |
| 73 | def __init__(self): |
| 74 | # Lists for job management, accessed via a property to ensure they're |
| 75 | # up to date.x |
| 76 | self._running = [] |
| 77 | self._completed = [] |
| 78 | self._dead = [] |
| 79 | # A dict of all jobs, so users can easily access any of them |
| 80 | self.all = {} |
| 81 | # For reporting |
| 82 | self._comp_report = [] |
| 83 | self._dead_report = [] |
| 84 | # Store status codes locally for fast lookups |
| 85 | self._s_created = BackgroundJobBase.stat_created_c |
| 86 | self._s_running = BackgroundJobBase.stat_running_c |
| 87 | self._s_completed = BackgroundJobBase.stat_completed_c |
| 88 | self._s_dead = BackgroundJobBase.stat_dead_c |
| 89 | self._current_job_id = 0 |
| 90 | |
| 91 | @property |
| 92 | def running(self): |
| 93 | self._update_status() |
| 94 | return self._running |
| 95 | |
| 96 | @property |
| 97 | def dead(self): |
| 98 | self._update_status() |