A subclass of threading.Thread whose run() method repeats. Use this class for most repeating tasks. It uses time.sleep() to wait for each interval, which isn't very responsive; that is, even if you call self.cancel(), you'll have to wait until the sleep() call finishes before the th
| 483 | |
| 484 | |
| 485 | class BackgroundTask(threading.Thread): |
| 486 | """A subclass of threading.Thread whose run() method repeats. |
| 487 | |
| 488 | Use this class for most repeating tasks. It uses time.sleep() to |
| 489 | wait for each interval, which isn't very responsive; that is, even |
| 490 | if you call self.cancel(), you'll have to wait until the sleep() |
| 491 | call finishes before the thread stops. To compensate, it defaults to |
| 492 | being daemonic, which means it won't delay stopping the whole |
| 493 | process. |
| 494 | """ |
| 495 | |
| 496 | def __init__(self, interval, function, args=[], kwargs={}, bus=None): |
| 497 | super(BackgroundTask, self).__init__() |
| 498 | self.interval = interval |
| 499 | self.function = function |
| 500 | self.args = args |
| 501 | self.kwargs = kwargs |
| 502 | self.running = False |
| 503 | self.bus = bus |
| 504 | |
| 505 | # default to daemonic |
| 506 | self.daemon = True |
| 507 | |
| 508 | def cancel(self): |
| 509 | self.running = False |
| 510 | |
| 511 | def run(self): |
| 512 | self.running = True |
| 513 | while self.running: |
| 514 | time.sleep(self.interval) |
| 515 | if not self.running: |
| 516 | return |
| 517 | try: |
| 518 | self.function(*self.args, **self.kwargs) |
| 519 | except Exception: |
| 520 | if self.bus: |
| 521 | self.bus.log('Error in background task thread function %r.' |
| 522 | % self.function, level=40, traceback=True) |
| 523 | # Quit on first error to avoid massive logs. |
| 524 | raise |
| 525 | |
| 526 | |
| 527 | class Monitor(SimplePlugin): |
no outgoing calls
no test coverage detected
searching dependent graphs…