The candygram module exports the following functions, classes, constants, and exceptions. Since the name candygram is a bit long, you would typically import the module in one of the following ways:
>>> from candygram import *
>>> import candygram as cg
func[, args...][, _processClass=Process]) |
'badarg'
ExitError if func is not
callable().
If you define a subclass of Process, you can instruct the spawn() function to create an instance of that class by passing the class with the optional _processClass keyword argument.
proc) |
'badarg'
ExitError if proc is not a Process
instance. Sends a 'noproc'
'EXIT'
signal to calling process if the
proc process is no longer alive.
When a process terminates, it sends an 'EXIT'
signal is to all of its
linked processes. If a process terminates normally, it sends a 'normal'
'EXIT'
signal to its linked processes.
All links are bidirectional. That is, if process A calls link(B)
, then if
process B terminates, it sends an 'EXIT'
signal to process A. Conversely,
if process A terminates, it likewise sends an 'EXIT'
signal to process B.
Refer to the processFlag() function for details about handling signals.
func[, args...][, _processClass=Process]) |
>>> proc = spawn(func, args...) >>> link(proc)
'badarg'
ExitError if
func is not callable().
If you define a subclass of Process, you can instruct the spawnLink() function to create an instance of that class by passing the class with the optional _processClass keyword argument.
proc) |
'badarg'
ExitError if proc is not a Process
instance.
proc) |
'badarg'
ExitError if proc is not a Process instance.
) |
) |
) |
proc, message) |
.send(
message)
. Raises a
'badarg'
ExitError if proc is not a Process
instance.
[proc, ]reason) |
When the proc argument is given, this function sends an 'EXIT'
signal to the process proc. This is not necessarily the same as sending an
'EXIT'
message to proc. They are the same if proc is trapping
exits. If proc is not trapping exits, however, the proc process
terminates and propagates the 'EXIT'
signal in turn to its linked
processes.
If the reason is the string 'kill'
, an untrappable 'EXIT'
signal is sent to the process. In other words, the proc process is
unconditionally killed.
Refer to the processFlag() function for details about trapping exits.
flag, option) |
'badarg'
ExitError if flag is not a
recognized flag value, or if option is not a recognized value for
flag.
Currently, 'trap_exit'
is the only recognized flag value. When
'trap_exit'
is set to True, 'EXIT'
signals arriving to
a process are converted to ('EXIT', from, reason)
messages, which can be
received as ordinary messages. If 'trap_exit'
is set to False,
the process exits if it receives an 'EXIT'
signal other than
'normal'
and propagates the 'EXIT'
signal to its linked processes.
Application processes should normally not trap exits.
) |
) |
) |
message) |
Sending a message is an asynchronous operation so the send() call does not wait for a Receiver to retrieve the message. Even if this process has already terminated, the system does not notify the sender. Messages are always delivered, and always in the same order they were sent.
message) |
>>> proc | ('knock-knock', 'candygram')
) |
pattern[, func[, args...]]) |
'badarg'
ExitError if func is not
callable().
If any of the args arguments is candygram.Message, then the receive() method replaces that argument with the matching message when it invokes func.
Refer to the receive() documentation for details about the mechanism by which it invokes the handlers.
pattern, funcWithArgs) |
__setitem__() is an alias to the addHandler() method so that developers can use a more Erlangy syntax to specify handlers. In Erlang, the ``pattern -> func'' syntax specifies pattern guards. For example:
>>> r = Receiver() >>> r['knock-knock', 'candygram'] = answer, 'From whom?'
pattern) |
__getitem__() is an alias to the addHandler() method so that developers can use a more Erlangy syntax to specify handlers. In Erlang, the ``pattern -> func'' syntax specifies pattern guards. For example:
>>> r = Receiver() >>> r['knock-knock', 'shark'] # ignore sharks
receiver) |
'badarg'
ExitError if receiver is not a
Receiver instance.
handlerReference) |
'badarg'
ExitError if handlerReference is not a proper reference, or if
it refers to a handler function that is not in the Receiver.
timeout[, func[, args...]]) |
'badarg'
ExitError if func is not callable() or if
timeout is not an integer. Raises an AssertionError if the
after() method has already been invoked.
If the receive() method does not find a matching message within timeout milliseconds, it invokes func with args and returns the result. When a handler function func is not specified, the receive() method returns None if it times out.
None of the args arguments should be candygram.Message, since there is no message to pass when a timeout occurs.
[timeout[, func[, args...]]]) |
If no message matches any of the patterns, or if the mailbox is empty, this
method blocks until the process receives a message that does match one of the
patterns, or until the given timeout has elapsed. If no timeout is
specified, this method blocks indefinitely. If a timeout is specified, it
is equivalent to calling after(timeout, func, args)
just prior to receive(). Raises a 'badarg'
ExitError if func is not callable() or if
timeout is not an integer. Also raises an AssertionError if a
timeout is specified and the after() method has already been
invoked.
[timeout[, func[, args...]]]) |
>>> def convert(): ... r = Receiver() ... r['one'] = lambda: 1 ... r['two'] = lambda: 2 ... r['three'] = lambda: 3 ... return r()
) |
... while True: ... result = receiver.receive() ... # do whatever with the result...
... for result in receiver: ... # do whatever with the result...
'EXIT'
errors from Erlang. If an Erlang function can
cause a failure under certain circumstances, then the corresponding Candygram
function raises an ExitError under the same circumstances.
A process also raises an ExitError in all of its linked processes
if it terminates for a reason other than 'normal'
.
'normal'
, it does not
immediately raise an ExitError in all linked processes. Candygram
defers the ExitError instead, and raises it the next time one of its
functions or methods is called. (Python does not allow you to unconditionally
interrupt a separate thread.)