The examples directory also contains a few other modules that demonstrate how you can use Candygram to perform some handy functions.
If you are not already familiar with Erlang, the best way to become familiar with Candygram is to read chapter 5 in Concurrent Programming in Erlang and follow along using the example programs located in the examples directory. If you are already familiar with Erlang, here are a few code snippets to give you a taste of Candygram.
>>> from candygram import * >>> import time >>> def proc_func(): ... r = Receiver() ... r['land shark'] = lambda m: 'Go Away ' + m, Message ... r['candygram'] = lambda m: 'Hello ' + m, Message ... for message in r: ... print message ... >>> proc = spawn(proc_func) >>> proc | 'land shark' >>> proc | 'candygram' >>> # Give the proc a chance to print its messages before termination: ... time.sleep(1)
Go Away land shark Hello candygram
The code above uses a rather Erlangy syntax. Here is a more Pythonic version that does the same:
>>> import candygram as cg >>> import time >>> def proc_func(): ... r = cg.Receiver() ... r.addHandler('land shark', shut_door, cg.Message) ... r.addHandler('candygram', open_door, cg.Message) ... for message in r: ... print message ... >>> def shut_door(name): ... return 'Go Away ' + name ... >>> def open_door(name): ... return 'Hello ' + name ... >>> proc = cg.spawn(proc_func) >>> proc.send('land shark') >>> proc.send('candygram') >>> # Give the proc a chance to print its messages before termination: ... time.sleep(1)
Lastly, here is an example with more elaborate patterns:
>>> from candygram import * >>> import time >>> def proc_func(name): ... r = Receiver() ... r['msg', Process, str] = print_string, name, Message ... r['msg', Process, str, Any] = print_any, name, Message ... r[Any] # Ignore any other messages ... for result in r: ... pass ... >>> def print_string(name, message): ... msg, process, string = message ... # 'msg' and 'process' are unused ... print '%s received: %s' % (name, string) ... >>> def print_any(name, message): ... msg, process, prefix, value = message ... # 'msg' and 'process' are unused ... print '%s received: %s %s' % (name, prefix, value) ... >>> a = spawn(proc_func, 'A') >>> b = spawn(proc_func, 'B') >>> a | ('msg', b, 'Girl Scout cookies') >>> a | 'plumber?' >>> a | ('msg', b, 'The meaning of life is:', 42) >>> # Give the proc a chance to print its messages before termination: ... time.sleep(1)
A received: Girl Scout cookies A received: The meaning of life is: 42