node module (begin code exploration from here!)

class pyrlang.node.Node(node_name: str, cookie: str, hidden: bool = False)

Bases: object

Implements an Erlang node which has a network name, a dictionary of processes and registers itself via EPMD. Node handles the networking asynchronously.

This is the root object of an Erlang node, it must be created first and must outlive all other objects it manages, for them to be accessible over the network.

Example:

  1. Create an async engine adapter:

    e = GeventEngine() # located in import Pyrlang

  2. Create a node class with a name and a cookie

    node = Pyrlang.Node("py@127.0.0.1", "COOKIE", engine=e)

  3. Start it via the engine adapter e.start(node)

  4. Now anything that you do (for example an infinite loop with

    e.sleep(1) in it, will give CPU time to the node.

demonitor_process(origin_pid, target, ref)

Locate the process target and remove the origin from its monitors_ collection. This does not trigger any notifications or signals to the origin.

Parameters
  • ref (term.reference.Reference) -- Reference which you received when setting up a monitor.

  • origin_pid (Pid) -- The process who was monitoring the target before

  • target (Pid or Atom) -- Name or pid of a monitor target process, possibly it does not exist

Raises

ProcessNotFound -- if target does not exist

destroy()

Closes incoming and outgoing connections and destroys the local node. This is Python, so some refs from running async handlers may remain.

async dist_command(receiver_node: str, message: tuple) → None

Locate the connection to the given node (a string). Place a tuple crafted by the caller into message box for Erlang dist_proto socket. It will pick up and handle the message whenever possible.

Parameters
  • receiver_node -- Name of a remote node

  • message -- A crafted tuple with command name and some more values

Raises

NodeException -- if unable to find or connect to the node

exit_process(sender, receiver, reason)

Delivers exit message to a local or remote process.

Get string cookie value for this node. TODO: Cookie per connection?

get_loop()
async handle_signals()
inbox_ = None

Contains Pyrlang's own messages to the local node.

Check each of processes pid1 and pid2 if they are local, mutually link them. Assume remote process handles its own linking.

Parameters
  • pid1 (term.pid.Pid) -- First pid

  • pid2 (term.pid.Pid) -- Second pid

  • local_only -- If set to True, linking to remote pids will send LINK message over dist_proto protocol

unsafe casting of link

you can't assume it has got effect when this function returns Convinience methot for being able to call it outside a async function

monitor_process(origin_pid: term.pid.Pid, target, ref=None)

Locate the process referenced by the target and place the origin pid into its monitors_ collection. When something happens to the target, a special message will be sent to the origin. Remote targets are supported.

Parameters
  • ref (None or term.reference.Reference) -- If not None, will be reused, else a new random ref will be generated.

  • origin_pid (term.pid.Pid) -- The (possibly remote) process who will be monitoring the target from now and wants to know when we exit.

  • target (term.pid.Pid or term.atom.Atom) -- Name or pid of a monitor target process.

Return type

term.reference.Reference

Raises

pyrlang.node.ProcessNotFoundError -- if target does not exist.

node_db = <pyrlang.node_db.NodeDB object>

All existing local Node objects indexed by node_name: str

node_name_ = None

Node name as seen on the network. Use full node names here: name@hostname

node_opts_ = None

Distribution options object with feature support flags packed into an integer. The remote node will receive these flags to know what features we can support.

on_exit_process(exiting_pid, reason)
pid_counter_ = None

An internal counter used to generate unique process ids

processes_ = None

Process dictionary which stores all the existing Process objects adressable by a pid.

Note

This creates a python reference to an object preventing its automatic garbage collection. In the end of its lifetime an object must be explicitly removed from this dictionary using Process.exit method on the process.

reg_names_ = None

Registered objects dictionary, which maps atoms to pids

register_dist_node(addr, proto)

add the protocol to dist nodes so that we can use it :param addr: :param proto: :return:

register_name(proc, name) → None

Add a name into registrations table (automatically removed when the referenced process is removed)

Parameters
  • proc (Process) -- The process to register

  • name (Atom) -- The name to register with

register_new_process(proc=None) → term.pid.Pid

Generate a new pid and add the process to the process dictionary.

Parameters

proc (Process or None) -- A new process which needs a pid, or None if you only need a fake pid

Returns

A new pid (does not modify the process in place, so please store the pid!)

run()
async send(sender, receiver, message) → None

Deliver a message to a pid or to a registered name. The pid may be located on another Erlang node.

Parameters
  • sender (term.pid.Pid or None) -- Message sender

  • receiver (term.pid.Pid or term.atom.Atom or Tuple[Atom, Pid or Atom]) -- Message receiver, a pid, or a name, or a tuple with node name and a receiver on the remote node.

  • message -- Any value which will be placed into the receiver inbox. Pyrlang processes use tuples but that is not enforced for your own processes.

send_nowait(sender, receiver, message) → None

Create task that sends the message

signal_wake_up(pid)

Mutually unlink two processes.

Parameters
  • pid1 (term.pid.Pid) -- First pid

  • pid2 (term.pid.Pid) -- Second pid

  • local_only -- If set to True, linking to remote pids will send UNLINK message over dist_proto protocol

unregister_dist_node(addr)

remove dist node (disconnected most likley) :param addr: :return:

where_is(ident)

Look up a registered name or pid.

Parameters

ident (term.atom.Atom or term.pid.Pid) -- an Atom or a Pid to convert to a Pid

Return type

term.pid.Pid

where_is_process(ident)

Look up a registered name or pid.

Return type

pyrlang.process.Process or None