EX21 - Remote shared objects
Python’s remote shared objects is one possibility to use asynchronous message passing in a distributed computing environment.
Question 1 - Distributed channels
A broker that offers a channel is online (available via Internet) at the address concurp1.isc.heia-fr.ch on port 5000.
There is a script ready for you in the code-snippets/253/ that is called cli_get.py. This script connects to the remote object server (running on the above described server, port 5000), and just wait for incoming messages (via a remote asynchronous channel) and displays them on the screen.
A small Python script sending a message in this channel can be found here: code-snippets/253/
Download the script to your local host and execute it with your personal message:
python3 cli_put.py <message>
Execute these two scripts and play with it.
Question 2 - Distributed semaphores
In different groups you should write a small Python scrip that runs in an endless loop. In this loop you try to get the critical section (CS) by acquiring a semaphore. Once in the CS you spend some (simulated nap) time, before leaving the CS again and restarting the loop. Don’t forget to print some debugging messages on your screens.
while True:
# no CS section
rem_semaphore.acquire()
# CS section
rem_semaphore.release()
All in the group should run the script at the same time. Of course, only one process can be in the CS at the time…
The broker: concurp1.isc.heia-fr.ch
Port: 5001 - 5009 (for the groups 1 - 9)
The semaphore object name: remote_semaphore
The authentication key: abracadabra (in binary ==> b'abracadabra')
Solution
Here a possible solution
...
# create the BaseManager instance, prepare for connection to the broker and register some objects
m = BaseManager(address=(conf.MANAGER_ADDRESS, conf.MANAGER_PORT), authkey=conf.MANAGER_PW)
m.register('remote_semaphore')
# now connect to the broker and create the remote objects
try:
m.connect()
rem_semaphore = m.remote_semaphore() # get the distributed queue object
...
while True:
print(f"Trying to acquiring the semaphore")
rem_semaphore.acquire()
print(f"Got the semaphore, I am in the CS")
#print(f"{rem_semaphore._value}")
sleep(NAP)
print(f"Returning the semaphore and quitting the CS")
rem_semaphore.release()