Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Commit

Permalink
okay, NOW multithreading works properly, and can i just say: holy fuc…
Browse files Browse the repository at this point in the history
…k it's SO MUCH FASTER
  • Loading branch information
Lynnesbian committed May 27, 2020
1 parent 9545442 commit ac4cb2a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ config.json
planning.txt
*.pyc
/debug
lynnesbian.json
lynnesbian.json
test.py
27 changes: 12 additions & 15 deletions app/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,19 @@ def make_post(args):
db.commit()
c.close()

def task_done(future):
try:
result = future.result() # blocks until results are ready
except TimeoutError as error:
if not future.silent: print("Timed out on {}.".format(future.function_data))

def do_in_pool(function, data, timeout=30, silent=False):
with ProcessPool(max_workers=cfg['service_threads']) as p:
index = 0
future = p.map(function, data)
iterator = future.result()

while True:
try:
result = next(iterator)
except StopIteration:
# all threads are done
break
except TimeoutError as error:
if not silent: print("Timed out on {}.".format(data[index]))
finally:
index += 1
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():
db = MySQLdb.connect(
Expand Down

0 comments on commit ac4cb2a

Please sign in to comment.