Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c3865b

Browse files
committed
Expand barrier example to show time-outs.
1 parent e0f1f32 commit 2c3865b

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

Doc/whatsnew/3.2.rst

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,6 @@ are suitable for use in loops. The separate *filling* and *draining* phases
847847
assure that all threads get released (drained) before any one of them can loop
848848
back and re-enter the barrier. The barrier fully resets after each cycle.
849849

850-
If any of the predecessor tasks can hang or be delayed, a barrier can be created
851-
with an optional *timeout* parameter. Then if the timeout period elapses before
852-
all the predecessor tasks reach the barrier point, all waiting threads are
853-
released and a :exc:`~threading.BrokenBarrierError` exception is raised.
854-
855850
Example of using barriers::
856851

857852
def get_votes(site):
@@ -870,6 +865,26 @@ is similar to one with :meth:`threading.Thread.join`, but the threads stay alive
870865
and continue to do work (summarizing ballots) after the barrier point is
871866
crossed.
872867

868+
If any of the predecessor tasks can hang or be delayed, a barrier can be created
869+
with an optional *timeout* parameter. Then if the timeout period elapses before
870+
all the predecessor tasks reach the barrier point, all waiting threads are
871+
released and a :exc:`~threading.BrokenBarrierError` exception is raised::
872+
873+
def get_votes(site):
874+
ballots = conduct_election(site)
875+
try:
876+
all_polls_closed.wait(timeout = midnight - time.now())
877+
except BrokenBarrerError:
878+
lockbox = seal_ballots(ballots)
879+
queue.put(lockbox)
880+
else:
881+
totals = summarize(ballots)
882+
publish(site, totals)
883+
884+
In this example, the barrier enforces a more robust rule. If some election
885+
sites do not finish before midnight, the barrier times-out and the ballots are
886+
sealed and deposited in a queue for later handling.
887+
873888
See `Barrier Synchronization Patterns
874889
<http://parlab.eecs.berkeley.edu/wiki/_media/patterns/paraplop_g1_3.pdf>`_ for
875890
more examples of how barriers can be used in parallel computing. Also, there is

0 commit comments

Comments
 (0)