5 Examples

There is a directory named examples within every distribution of Candygram. This directory contains the Candygram equivalents of all the sample programs found in chapter 5 and sections 7.2, 7.3, and 7.5 of Concurrent Programming in Erlang. The files named program_X.X.py are direct translations of the Erlang programs. The files named program_X.X_alt.py are alternate, more liberal translations that use more Pythonic idioms. Note: Since the CPython interpreter does not perform tail-call optimization, the tail-recursive style of the direct translations is not a recommended practice.

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)
Running the code above produces the following output:
Go Away land shark
Hello candygram

Note: If this code is run as a script, the Python interpreter will exit after the last line is evaluated, killing any spawned processes. That is why the last line is a call to the sleep() function, so that the spawned process has an opportunity to print its messages before the interpreter exits and kills it.

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)
Running the code above produces the following output:
A received: Girl Scout cookies
A received: The meaning of life is: 42

SourceForge.net Logo