def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
... # Define all widgets, setup worker threadpool etc.
# Refresh threads occasionally - My bad implementation
self.rtimer.setInterval(1_000)
self.rtimer.timeout.connect(self.refreshThreads) # Recheck for threads that need to be refresh()'d every second.
# Auto GUI List Refreshing
self.ltimer.setInterval(100)
self.ltimer.timeout.connect(self.refreshList) # Refresh GUI list every 0.1 seconds
# Start the timers.
self.rtimer.start()
self.ltimer.start()
# Thread objects (not execution threads!)
# self.threads - A list of None and Thread objects which represent the 'active' threads in the list.
# self.schedule - A dictionary of the UNIX timestamps the threads should be refreshed at.
# self.working - A dictionary of bool values representing whether a thread is currently being refresh()'d.
self.threads, self.schedule, self.working = [], {}, {}
self.addThreads(n=10) # Add 10 threads to the list.
def refreshThreads(self):
# Schedules new workers.
workers = []
now = time.time()
threads = filter(lambda t: t is not None and not self.working.get(t.id) and self.schedule[t.id] <= time.time(),
self.threads)
for thread in threads:
self.working[thread.id] = True
print(f'Starting {thread}.refresh')
worker = Worker(thread.refresh)
worker.signals.result.connect(
self.processThreadRefresh) # Once finished, the result of the refresh should be sent back.
worker.signals.finished.connect(self.refreshList)
workers.append(worker)
# Add all the Workers to the threadpool and begin working.
if workers:
# print(f'Threaded {len(workers)} Workers')
for worker in workers:
self.threadpool.start(worker)
def processThreadRefresh(self, result):
# Used as a way to remove threads from the running if they need to.
# Thread callback once it returns it's result/decision.
active, id = result
self.working[id] = False # Stop 'working' on a thread
if active:
print(f'Rescheduling {self.threads[id]}')
self.schedule[id] = self.threads[id].getRefreshTime()
else:
print(f'Removing {self.threads[id]}')
self.threads[
id] = None # Not really sure what to do here. double dictionary? Is a infinite list of None's okay?
del self.schedule[id]
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
... # Define all widgets, setup worker threadpool etc.
# Refresh threads occasionally - My bad implementation
self.rtimer = QTimer()
self.rtimer.setInterval(1_000)
self.rtimer.timeout.connect(self.refreshThreads) # Recheck for threads that need to be refresh()'d every second.
# Auto GUI List Refreshing
self.ltimer = QTimer()
self.ltimer.setInterval(100)
self.ltimer.timeout.connect(self.refreshList) # Refresh GUI list every 0.1 seconds
# Start the timers.
self.rtimer.start()
self.ltimer.start()
# Thread objects (not execution threads!)
# self.threads - A list of None and Thread objects which represent the 'active' threads in the list.
# self.schedule - A dictionary of the UNIX timestamps the threads should be refreshed at.
# self.working - A dictionary of bool values representing whether a thread is currently being refresh()'d.
self.threads, self.schedule, self.working = [], {}, {}
self.addThreads(n=10) # Add 10 threads to the list.
def refreshThreads(self):
# Schedules new workers.
workers = []
now = time.time()
threads = filter(lambda t: t is not None and not self.working.get(t.id) and self.schedule[t.id] <= time.time(),
self.threads)
for thread in threads:
self.working[thread.id] = True
print(f'Starting {thread}.refresh')
worker = Worker(thread.refresh)
worker.signals.result.connect(
self.processThreadRefresh) # Once finished, the result of the refresh should be sent back.
worker.signals.finished.connect(self.refreshList)
workers.append(worker)
# Add all the Workers to the threadpool and begin working.
if workers:
# print(f'Threaded {len(workers)} Workers')
for worker in workers:
self.threadpool.start(worker)
def processThreadRefresh(self, result):
# Used as a way to remove threads from the running if they need to.
# Thread callback once it returns it's result/decision.
active, id = result
self.working[id] = False # Stop 'working' on a thread
if active:
print(f'Rescheduling {self.threads[id]}')
self.schedule[id] = self.threads[id].getRefreshTime()
else:
print(f'Removing {self.threads[id]}')
self.threads[
id] = None # Not really sure what to do here. double dictionary? Is a infinite list of None's okay?
del self.schedule[id]
To copy to clipboard, switch view to plain text mode
Bookmarks