How it works...

The Barrier object provides one of the Python synchronization techniques with which single or multiple threads wait until a point in a set of activities and make progress together. 

In the main program, the Barrier object (that is, synchronizer) is defined through the following statement:

synchronizer = Barrier(2)

Note that the number 2 within the parentheses represents the number of processes that the barrier should wait upon.

Then, we implement a set of four processes, but only for the p1 and p2 processes. Note that synchronizer is passed as an argument:

Process(name='p1 - test_with_barrier'
,target=test_with_barrier,
args=(synchronizer,serializer)).start()
Process(name='p2 - test_with_barrier'
,target=test_with_barrier,
args=(synchronizer,serializer)).start()

Indeed, in the body of the test_with_barrier function, the barrier's wait() method is used in order to synchronize the processes:

synchronizer.wait()

By running the script, we can see that the p1 and p2 processes print out the same timestamps as expected:

> python processes_barrier.py
process p4 - test_without_barrier ----> 2019-03-03 08:58:06.159882
process p3 - test_without_barrier ----> 2019-03-03 08:58:06.144257
process p1 - test_with_barrier ----> 2019-03-03 08:58:06.175505
process p2 - test_with_barrier ----> 2019-03-03 08:58:06.175505
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.32.230