I have been trying to learn how multiprocessing works in python, and have created a very simple script to test my understanding.
Everything seems to work fine, except for the fact that, within a process, none of value assignments that happen after a while loop appear to be completed.
If I replace the while loop with a for loop, or remove it completely, everything seems to work as expected.
Here is the main.py:
from timepieces import Stopwatch
def run():
watch = Stopwatch()
watch.start()
can_count = 0 if (raw_input('Press q to stop and see seconds counted') == 'q') else 1
watch._can_count.value = can_count
print watch.get_seconds()
if __name__ == "__main__":
run()
And the timepieces.py file (contains class that creates and controls process):
from multiprocessing import Process, Value
import time
import math
class Stopwatch:
_count_val = None
_proc = None
_can_count = None
def count_seconds(self, can_count, count_val):
seconds = 0
count_val.value = 23
#print can_count.value == 1
while can_count.value == 1:
seconds += 1
count_val.value = seconds
def start(self):
self._count_val = Value('i', 0)
self._can_count = Value('i', 1)
self._proc = Process(target = self.count_seconds, args = (self._can_count, self._count_val))
self._proc.start()
def get_seconds(self):
return self._count_val.value
Any thoughts are appreciated.