update in parallel

This commit is contained in:
Lynne Megido 2020-08-23 23:28:48 +10:00
parent 1fd1fb21d0
commit b83f45d15e
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
2 changed files with 32 additions and 15 deletions

View File

@ -33,6 +33,8 @@ import functions
from listing import YahooAuctionsItem, JST
from settings import BuypeebSettings
app = None
isWindows = sys.platform.startswith('win')
if isWindows:
@ -40,6 +42,14 @@ if isWindows:
else:
settingsLocation = path.expanduser("~/.config/Lynnear Software/buypeeb/") # dotfiles in ~ need to die. begone dot
def watchlist_update_done(future):
try:
print(future.result())
app.updateListItem(future.result()[0], future.result()[1])
# app.renderList()
except:
raise
class WatchlistUpdater(Thread):
def __init__(self, app, update_all: bool = False):
Thread.__init__(self)
@ -47,18 +57,19 @@ class WatchlistUpdater(Thread):
self.update_all = update_all
def run(self):
# TODO: make this happen in parallel
if self.update_all:
for id, item in self.app.settings.watchlist.items():
if not (item.ready or item.updating):
item.update()
with ProcessPool(max_workers = 4) as pool:
if self.update_all:
for id, item in self.app.settings.watchlist.items():
if not (item.ready or item.updating):
future = pool.schedule(item.update)
future.add_done_callback(watchlist_update_done)
else:
for item in self.app.settings.outdated_items():
if not(item.ready or item.updating):
item.update()
else:
for item in self.app.settings.outdated_items():
if not(item.ready or item.updating):
future = pool.schedule(item.update)
future.add_done_callback(watchlist_update_done)
self.app.renderList()
class BuypeebApp:
# SETUP
@ -167,14 +178,19 @@ class BuypeebApp:
self.updateListTimes()
def updateListItem(self, id: str):
def updateListItem(self, id: str, item_list = None):
item = self.settings.watchlist[id]
for listing in self.items:
if listing[3] == id:
# print(f"Updating {id} ({item.name}, {item.price_aud()}, {item.ready})")
treeIter = Gtk.TreeModel.get_iter(self.items, listing.path)
self.items.set(treeIter, 0, item.name)
self.items.set(treeIter, 1, item.price_aud())
self.items.set(treeIter, 2, item.ending_at())
if item_list != None:
for i in range(0, 3):
self.items.set(treeIter, i, item_list[i])
else:
self.items.set(treeIter, 0, item.name)
self.items.set(treeIter, 1, item.price_aud())
self.items.set(treeIter, 2, item.ending_at())
def updateListTimes(self):
@ -202,7 +218,6 @@ class BuypeebApp:
self.builder.get_object("lblSelectedName").set_label("buypeeb")
# BUTTON CLICKS
def btnAddClicked(self, widget):

View File

@ -106,6 +106,8 @@ class YahooAuctionsItem:
self.ready = True
self.updating = False
return self.id, [self.name, self.price_aud(), self.ending_at()]
def price_jpy(self):
return f"¥{self.price:.0f}"