Removed not needed lock to speed up

This commit is contained in:
M1ha 2019-01-23 13:47:34 +05:00
parent 1a4ed0ddf6
commit e8549b9509

View File

@ -1,6 +1,6 @@
import datetime import datetime
from queue import Queue, Empty from queue import Queue, Empty
from threading import Thread, Lock from threading import Thread
import os import os
from itertools import chain from itertools import chain
@ -193,7 +193,6 @@ def exec_in_parallel(func: Callable, args_queue: Queue, threads_count: Optional[
:return: A list of results. Order of results is not guaranteed. Element types depends func return type. :return: A list of results. Order of results is not guaranteed. Element types depends func return type.
""" """
results = [] results = []
results_lock = Lock()
# If thread_count is not given, we execute all tasks in parallel. # If thread_count is not given, we execute all tasks in parallel.
# If queue has less elements than threads_count, take queue size. # If queue has less elements than threads_count, take queue size.
@ -214,12 +213,12 @@ def exec_in_parallel(func: Callable, args_queue: Queue, threads_count: Optional[
# Execute function # Execute function
local_res = func(*args, **kwargs) local_res = func(*args, **kwargs)
# Write result in lock # Write result. appending a list is thread safe operation according to:
with results_lock: # http://effbot.org/pyfaq/what-kinds-of-global-value-mutation-are-thread-safe.htm
results.append(local_res) results.append(local_res)
# Mark task as complete
args_queue.task_done() args_queue.task_done()
except Empty: except Empty:
# No data in queue, finish worker thread # No data in queue, finish worker thread
finished = True finished = True