default=6,
help='number of workers to start'
)
- parser.add_argument(
- '--trials-before-success',
- type=int,
- default=0,
- help='number of attempts each workers need to make before getting the correct answer'
- ' (i.e. number of simulated failures)'
- )
return parser
value: int
-class CalculationError(RuntimeError):
- """ Fake calculation error
- """
- pass
-
-
-def worker_context_manager(mutex: Mutex, trials_before_success: int, result: ResultHolder):
+def worker_context_manager(mutex: Mutex, result: ResultHolder):
""" Worker that uses a context manager to acquire/release the shared mutex
:param mutex: Shared mutex that guards read/write access to the shared result
- :param trials_before_success: Number of simulated calculation failures before success
:param result: Shared result which will be updated by the worker
"""
this_actor.info(f"I just started")
- for trial in range(trials_before_success + 1):
- try:
- with mutex:
- this_actor.info(f"acquired the mutex with context manager")
- this_actor.sleep_for(1)
- if trial < trials_before_success:
- raise CalculationError("did not manage to find the correct answer")
- result.value += 1
- this_actor.info(f"updated shared result, which is now {result.value}")
- except CalculationError as e:
- this_actor.warning(f"ran in trouble while calculating: {e}. Will retry shortly.")
- finally:
- this_actor.info(f"released the mutex after leaving the context manager")
+ with mutex:
+ this_actor.info(f"acquired the mutex with context manager")
+ result.value += 1
+ this_actor.info(f"updated shared result, which is now {result.value}")
+ this_actor.info(f"released the mutex after leaving the context manager")
this_actor.info("Bye now!")
-def worker(mutex: Mutex, trials_before_success: int, result: ResultHolder):
+def worker(mutex: Mutex, result: ResultHolder):
""" Worker that manually acquires/releases the shared mutex
:param mutex: Shared mutex that guards read/write access to the shared result
- :param trials_before_success: Number of simulated calculation failures before success
:param result: Shared result which will be updated by the worker
"""
this_actor.info(f"I just started")
- for trial in range(trials_before_success + 1):
- try:
- mutex.lock()
- this_actor.info(f"acquired the mutex manually")
- this_actor.sleep_for(1)
- if trial < trials_before_success:
- raise CalculationError("did not manage to find the correct answer")
- result.value += 1
- this_actor.info(f"updated shared result, which is now {result.value}")
- except CalculationError as e:
- this_actor.warning(f"ran in trouble while calculating: {e}. Will retry shortly.")
- finally:
- this_actor.info(f"released the mutex manually")
- mutex.unlock()
+ mutex.lock()
+ this_actor.info(f"acquired the mutex manually")
+ result.value += 1
+ this_actor.info(f"updated shared result, which is now {result.value}")
+ mutex.unlock()
+ this_actor.info(f"released the mutex manually")
this_actor.info("Bye now!")
Host.by_name("Jupiter" if use_worker_context_manager else "Tremblay"),
worker_context_manager if use_worker_context_manager else worker,
mutex,
- settings.trials_before_success,
result
)
)
- [actor.join() for actor in actors]
+ this_actor.sleep_for(10)
this_actor.info(f"The final result is: {result.value}")
p Testing Mutex
$ ${pythoncmd:=python3} ${PYTHON_TOOL_OPTIONS:=} ${bindir:=.}/synchro-mutex.py --platform ${platfdir}/two_hosts.xml --workers 0 "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
->[ 0.000000] (1:master@Tremblay) The final result is: 0
+>[ 10.000000] (1:master@Tremblay) The final result is: 0
$ ${pythoncmd:=python3} ${PYTHON_TOOL_OPTIONS:=} ${bindir:=.}/synchro-mutex.py --platform ${platfdir}/two_hosts.xml --workers 1 "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
>[ 0.000000] (2:worker-0(mgr)@Jupiter) I just started
>[ 0.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 1.000000] (2:worker-0(mgr)@Jupiter) updated shared result, which is now 1
->[ 1.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 1.000000] (2:worker-0(mgr)@Jupiter) Bye now!
->[ 1.000000] (1:master@Tremblay) The final result is: 1
-
-$ ${pythoncmd:=python3} ${PYTHON_TOOL_OPTIONS:=} ${bindir:=.}/synchro-mutex.py --platform ${platfdir}/two_hosts.xml --workers 1 --trials-before-success 5 "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
->[ 0.000000] (2:worker-0(mgr)@Jupiter) I just started
->[ 0.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 1.000000] (2:worker-0(mgr)@Jupiter) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 1.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 1.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 2.000000] (2:worker-0(mgr)@Jupiter) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 2.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 2.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 3.000000] (2:worker-0(mgr)@Jupiter) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 3.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 3.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 4.000000] (2:worker-0(mgr)@Jupiter) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 4.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 4.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 5.000000] (2:worker-0(mgr)@Jupiter) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 5.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 5.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 6.000000] (2:worker-0(mgr)@Jupiter) updated shared result, which is now 1
->[ 6.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 6.000000] (2:worker-0(mgr)@Jupiter) Bye now!
->[ 6.000000] (1:master@Tremblay) The final result is: 1
+>[ 0.000000] (2:worker-0(mgr)@Jupiter) updated shared result, which is now 1
+>[ 0.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
+>[ 0.000000] (2:worker-0(mgr)@Jupiter) Bye now!
+>[ 10.000000] (1:master@Tremblay) The final result is: 1
$ ${pythoncmd:=python3} ${PYTHON_TOOL_OPTIONS:=} ${bindir:=.}/synchro-mutex.py --platform ${platfdir}/two_hosts.xml --workers 5 "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
>[ 0.000000] (2:worker-0(mgr)@Jupiter) I just started
>[ 0.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
+>[ 0.000000] (2:worker-0(mgr)@Jupiter) updated shared result, which is now 1
>[ 0.000000] (3:worker-1@Tremblay) I just started
+>[ 0.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
+>[ 0.000000] (2:worker-0(mgr)@Jupiter) Bye now!
+>[ 0.000000] (3:worker-1@Tremblay) acquired the mutex manually
+>[ 0.000000] (3:worker-1@Tremblay) updated shared result, which is now 2
>[ 0.000000] (4:worker-2(mgr)@Jupiter) I just started
+>[ 0.000000] (3:worker-1@Tremblay) released the mutex manually
+>[ 0.000000] (3:worker-1@Tremblay) Bye now!
+>[ 0.000000] (4:worker-2(mgr)@Jupiter) acquired the mutex with context manager
+>[ 0.000000] (4:worker-2(mgr)@Jupiter) updated shared result, which is now 3
>[ 0.000000] (5:worker-3@Tremblay) I just started
+>[ 0.000000] (4:worker-2(mgr)@Jupiter) released the mutex after leaving the context manager
+>[ 0.000000] (4:worker-2(mgr)@Jupiter) Bye now!
+>[ 0.000000] (5:worker-3@Tremblay) acquired the mutex manually
+>[ 0.000000] (5:worker-3@Tremblay) updated shared result, which is now 4
>[ 0.000000] (6:worker-4(mgr)@Jupiter) I just started
->[ 1.000000] (2:worker-0(mgr)@Jupiter) updated shared result, which is now 1
->[ 1.000000] (3:worker-1@Tremblay) acquired the mutex manually
->[ 1.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 1.000000] (2:worker-0(mgr)@Jupiter) Bye now!
->[ 2.000000] (3:worker-1@Tremblay) updated shared result, which is now 2
->[ 2.000000] (3:worker-1@Tremblay) released the mutex manually
->[ 2.000000] (4:worker-2(mgr)@Jupiter) acquired the mutex with context manager
->[ 2.000000] (3:worker-1@Tremblay) Bye now!
->[ 3.000000] (4:worker-2(mgr)@Jupiter) updated shared result, which is now 3
->[ 3.000000] (5:worker-3@Tremblay) acquired the mutex manually
->[ 3.000000] (4:worker-2(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 3.000000] (4:worker-2(mgr)@Jupiter) Bye now!
->[ 4.000000] (5:worker-3@Tremblay) updated shared result, which is now 4
->[ 4.000000] (5:worker-3@Tremblay) released the mutex manually
->[ 4.000000] (6:worker-4(mgr)@Jupiter) acquired the mutex with context manager
->[ 4.000000] (5:worker-3@Tremblay) Bye now!
->[ 5.000000] (6:worker-4(mgr)@Jupiter) updated shared result, which is now 5
->[ 5.000000] (6:worker-4(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 5.000000] (6:worker-4(mgr)@Jupiter) Bye now!
->[ 5.000000] (1:master@Tremblay) The final result is: 5
-
-$ ${pythoncmd:=python3} ${PYTHON_TOOL_OPTIONS:=} ${bindir:=.}/synchro-mutex.py --platform ${platfdir}/two_hosts.xml --workers 3 --trials-before-success 2 "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
->[ 0.000000] (2:worker-0(mgr)@Jupiter) I just started
->[ 0.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 0.000000] (3:worker-1@Tremblay) I just started
->[ 0.000000] (4:worker-2(mgr)@Jupiter) I just started
->[ 1.000000] (3:worker-1@Tremblay) acquired the mutex manually
->[ 1.000000] (2:worker-0(mgr)@Jupiter) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 1.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 2.000000] (3:worker-1@Tremblay) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 2.000000] (3:worker-1@Tremblay) released the mutex manually
->[ 2.000000] (4:worker-2(mgr)@Jupiter) acquired the mutex with context manager
->[ 3.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 3.000000] (4:worker-2(mgr)@Jupiter) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 3.000000] (4:worker-2(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 4.000000] (3:worker-1@Tremblay) acquired the mutex manually
->[ 4.000000] (2:worker-0(mgr)@Jupiter) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 4.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 5.000000] (3:worker-1@Tremblay) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 5.000000] (3:worker-1@Tremblay) released the mutex manually
->[ 5.000000] (4:worker-2(mgr)@Jupiter) acquired the mutex with context manager
->[ 6.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[ 6.000000] (4:worker-2(mgr)@Jupiter) ran in trouble while calculating: did not manage to find the correct answer. Will retry shortly.
->[ 6.000000] (4:worker-2(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 7.000000] (2:worker-0(mgr)@Jupiter) updated shared result, which is now 1
->[ 7.000000] (3:worker-1@Tremblay) acquired the mutex manually
->[ 7.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 7.000000] (2:worker-0(mgr)@Jupiter) Bye now!
->[ 8.000000] (3:worker-1@Tremblay) updated shared result, which is now 2
->[ 8.000000] (3:worker-1@Tremblay) released the mutex manually
->[ 8.000000] (4:worker-2(mgr)@Jupiter) acquired the mutex with context manager
->[ 8.000000] (3:worker-1@Tremblay) Bye now!
->[ 9.000000] (4:worker-2(mgr)@Jupiter) updated shared result, which is now 3
->[ 9.000000] (4:worker-2(mgr)@Jupiter) released the mutex after leaving the context manager
->[ 9.000000] (4:worker-2(mgr)@Jupiter) Bye now!
->[ 9.000000] (1:master@Tremblay) The final result is: 3
+>[ 0.000000] (5:worker-3@Tremblay) released the mutex manually
+>[ 0.000000] (5:worker-3@Tremblay) Bye now!
+>[ 0.000000] (6:worker-4(mgr)@Jupiter) acquired the mutex with context manager
+>[ 0.000000] (6:worker-4(mgr)@Jupiter) updated shared result, which is now 5
+>[ 0.000000] (6:worker-4(mgr)@Jupiter) released the mutex after leaving the context manager
+>[ 0.000000] (6:worker-4(mgr)@Jupiter) Bye now!
+>[ 10.000000] (1:master@Tremblay) The final result is: 5