Credit: Michael Hobbs
You want to write a multithreaded application, using, as the synchronization and communication primitive, a simple yet powerful message-passing paradigm.
lets you use concurrent programming semantics that are essentially
equivalent to those of the Erlang language. To use
candygram, you start by defining appropriate
classes, such as the following one, to model your threads'
import candygram as cg class ExampleThread(object): """A thread-class with just a single counter value and a stop flag.""" def _ _init_ _(self): """ Initialize the counter to 0, the running-flag to True. """ self.val = 0 self.running = True def increment(self): """ Increment the counter by one. """ self.val += 1 def sendVal(self, msg): """ Send current value of counter to requesting thread. """ req = msg req.send((cg.self( ), self.val)) def setStop(self): """ Set the running-flag to False. """ self.running = False def run(self): """ The entry point of the thread. """ # Register the handler functions for various messages: r = cg.Receiver( ) r.addHandler('increment', self.increment) r.addHandler((cg.Process, 'value'), self.sendVal, cg.Message) r.addHandler('stop', self.setStop) # Keep handling new messages until a stop has been requested while self.running: r.receive( )
To start a thread running this code under
counter = cg.spawn(ExampleThread( ).run)
To handle the
counter thread's ...