1
0
Fork 0
mirror of https://github.com/Lynnesbian/FediBooks/ synced 2024-11-25 08:38:59 +00:00

okay, NOW multithreading works properly, and can i just say: holy fuck it's SO MUCH FASTER

This commit is contained in:
Lynne Megido 2020-05-27 21:51:58 +10:00
parent 954544205e
commit ac4cb2ab7a
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
2 changed files with 14 additions and 16 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ planning.txt
*.pyc *.pyc
/debug /debug
lynnesbian.json lynnesbian.json
test.py

View file

@ -191,22 +191,19 @@ def make_post(args):
db.commit() db.commit()
c.close() c.close()
def do_in_pool(function, data, timeout=30, silent=False): def task_done(future):
with ProcessPool(max_workers=cfg['service_threads']) as p: try:
index = 0 result = future.result() # blocks until results are ready
future = p.map(function, data) except TimeoutError as error:
iterator = future.result() if not future.silent: print("Timed out on {}.".format(future.function_data))
while True: def do_in_pool(function, data, timeout=30, silent=False):
try: with ProcessPool(max_workers=5, max_tasks=10) as pool:
result = next(iterator) for i in data:
except StopIteration: future = pool.schedule(function, args=[i], timeout=timeout)
# all threads are done future.silent = silent
break future.function_data = i
except TimeoutError as error: future.add_done_callback(task_done)
if not silent: print("Timed out on {}.".format(data[index]))
finally:
index += 1
def get_key(): def get_key():
db = MySQLdb.connect( db = MySQLdb.connect(