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:
parent
954544205e
commit
ac4cb2ab7a
2 changed files with 14 additions and 16 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,3 +4,4 @@ planning.txt
|
||||||
*.pyc
|
*.pyc
|
||||||
/debug
|
/debug
|
||||||
lynnesbian.json
|
lynnesbian.json
|
||||||
|
test.py
|
||||||
|
|
|
@ -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:
|
|
||||||
index = 0
|
|
||||||
future = p.map(function, data)
|
|
||||||
iterator = future.result()
|
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
try:
|
||||||
result = next(iterator)
|
result = future.result() # blocks until results are ready
|
||||||
except StopIteration:
|
|
||||||
# all threads are done
|
|
||||||
break
|
|
||||||
except TimeoutError as error:
|
except TimeoutError as error:
|
||||||
if not silent: print("Timed out on {}.".format(data[index]))
|
if not future.silent: print("Timed out on {}.".format(future.function_data))
|
||||||
finally:
|
|
||||||
index += 1
|
def do_in_pool(function, data, timeout=30, silent=False):
|
||||||
|
with ProcessPool(max_workers=5, max_tasks=10) as pool:
|
||||||
|
for i in data:
|
||||||
|
future = pool.schedule(function, args=[i], timeout=timeout)
|
||||||
|
future.silent = silent
|
||||||
|
future.function_data = i
|
||||||
|
future.add_done_callback(task_done)
|
||||||
|
|
||||||
def get_key():
|
def get_key():
|
||||||
db = MySQLdb.connect(
|
db = MySQLdb.connect(
|
||||||
|
|
Loading…
Reference in a new issue