У меня проблема с тем, что мне нужно x
потоков для ожидания, пока они все не достигнут точки синхронизации Мое решение использует метод synchronise
ниже, который вызывается каждой поточной функцией, когда им нужно синхронизироваться.
Есть ли лучший способ сделать это?
thread_count = 0
semaphore = threading.Semaphore()
event = threading.Event()
def synchronise(count):
""" All calls to this method will block until the last (count) call is made """
with semaphore:
thread_count += 1
if thread_count == count:
event.set()
event.wait()
def threaded_function():
# Do something
# Block until 4 threads have reached this point
synchronise(4)
# Continue doing something else