mirror of
https://github.com/mynodebtc/mynode.git
synced 2024-11-16 02:12:49 +00:00
29 lines
788 B
Python
29 lines
788 B
Python
import threading
|
|
import time
|
|
import os
|
|
|
|
class BackgroundThread(threading.Thread):
|
|
|
|
def __init__(self, run_function, rate=30):
|
|
threading.Thread.__init__(self)
|
|
|
|
# The shutdown_flag is a threading.Event object that
|
|
# indicates whether the thread should be terminated.
|
|
self.shutdown_flag = threading.Event()
|
|
self.run_function = run_function
|
|
self.rate = rate
|
|
self.pid = 0
|
|
|
|
# ... Other thread setup code here ...
|
|
|
|
def run(self):
|
|
print('Thread #%s started' % self.ident)
|
|
|
|
self.pid = os.getpid()
|
|
|
|
while not self.shutdown_flag.is_set():
|
|
self.run_function()
|
|
time.sleep(self.rate)
|
|
|
|
# ... Clean shutdown code here ...
|
|
print('Thread #%s stopped' % self.ident) |