pyrlang.process

Python processes with a pid, an inbox, links and monitors.

class pyrlang.process.BacklogOwner(*args, **kwargs)

Bases: Protocol

A connection that counts the messages it delivered into inboxes and that no process has taken out yet (its receive backlog, for flow control).

backlog_add(nbytes)

A message of nbytes went into an inbox.

backlog_release(nbytes)

A message of nbytes was taken out of an inbox.

pyrlang.process.delivering(owner, nbytes=0)

Make messages put into inboxes from now on count towards the backlog of owner (None: towards no connection).

class pyrlang.process.Process(passive=False)

Bases: object

Implements Erlang process semantic and lifetime. Registers itself in the process registry, can receive and send messages. To optionally register self with a name, call node.register_name(self, term.Atom('fgsfds'))

Subclass it and handle incoming messages in handle_one_inbox_message(self, msg).

Note

Only a Process can serve as a target for sending messages, for linking and monitoring. You do not need to create a Process for simple one-way interactions with remote Erlang nodes.

node_db = <pyrlang.node_db.NodeDB object>
passive_: bool

Having passive=True will only wake up this Process when a message arrives, to handle it, otherwise it will not get any CPU time for any empty polling loops. Having passive=False will run process_loop() polling inbox.

node_name_: str

Convenience field to see the Node

inbox_: Queue[Any]

Message queue. Messages are detected by the _run loop and handled one by one in handle_one_inbox_message(). Messages from another node that wait here hold back reading from that node’s connection once there are too many (see Node).

inbox_rejects_: Queue[Any]

Used for selective receives. Stores messages which did not match while the rest of the inbox is scanned.

pid_

Process identifier for this object. Remember that when creating a process, it registers itself in the node, and this creates a reference. References prevent an object from being garbage collected. To destroy a process, get rid of this extra reference by calling exit() and telling it the cause of its death.

async process_loop()

Polls inbox in an endless loop.

Note

This will not be executed if the process was constructed with passive=True (the default). Passive processes should read their inbox directly from self.inbox_.

async receive(match=None, timeout=None, timeout_callback=None)

The next message that matches, as Erlang’s selective receive; messages that do not match stay in the inbox.

Parameters:
  • match (Match | None) – the patterns to try; default: any message

  • timeout (float | None) – seconds to wait; None or 0 waits for ever

  • timeout_callback (Callable[[], Any] | None) – called on a timeout, and its result returned

Raises:

pyrlang.errors.ProcessTimeoutError – on a timeout without a callback

Returns:

what the handler of the matching pattern returns

Return type:

Any

async handle_signals()

Called from Node if the Node knows that there’s a signal waiting to be handled.

handle_inbox()

Do not override handle_inbox, instead go for handle_one_inbox_message :returns: How many messages have been handled

handle_one_inbox_message(msg)

Override this method to handle new incoming messages.

handle_message_from(sender, msg)

Override this instead of handle_one_inbox_message to also learn who sent the message: the sender’s pid, or None if it is not known (a sender that gave none; a remote sender when the message came as SEND, which carries no sender). Called for passive processes only; a process with an inbox gets messages without their senders.

deliver_message(msg, sender=None)

Places message into the inbox, or delivers it immediately to a handler (if process is passive).

alias()

Create a process alias (EEP 53): a reference that can be used as a destination of Node.send here and of ! on Erlang nodes, until unalias() is called or the process exits. Useful as the reply address of a request: late replies to a removed alias are dropped instead of piling up in the inbox.

unalias(alias)

Deactivate an alias of this process.

Returns:

True if it was an active alias of this process

Return type:

bool

The processes this one is linked to (active links only).

monitors()

The monitors this process holds: reference -> monitored pid, as Erlang’s process_info(P, monitors).

monitored_by()

The monitors on this process: reference -> monitoring pid, as Erlang’s process_info(P, monitored_by).

Pids on node_name this process has link information about, active or being unlinked.

monitors_to_node(node_name)

The monitors this process holds on processes of node_name.

forget_monitors_from_node(node_name)

The connection to node_name is gone: the monitors its processes held on this one end without a message, as in OTP.

Links pid to this process. Please use Node method link() for proper linking.

Unlinks pid from this process at once, without the unlink protocol. Please use Node method unlink() for proper unlinking.

This process links to pid: the link becomes active and an outstanding unlink is forgotten.

Returns:

True if a LINK signal has to be sent (the link was not active)

Return type:

bool

A LINK signal from pid: creates an active link, unless there is link information already (an unacknowledged unlink stays so).

This process unlinks pid: the link becomes inactive until the other side acknowledges unlink_id.

Returns:

True if an UNLINK_ID signal has to be sent (the link was active)

Return type:

bool

An UNLINK_ID signal from pid: an active link is removed; with an unlink of our own outstanding nothing changes.

An UNLINK_ID_ACK signal from pid: ends our unlink if it is the one with unlink_id.

An exit signal due to the link with pid (it exited, or the connection to its node was lost): the link information is removed.

Returns:

True if the link was active, so the signal has to be acted on

Return type:

bool

exit(reason=None)

Marks the object as exiting with the reason, informs links and monitors and unregisters the object from the node process dictionary.

get_node()

The node this process belongs to.

add_monitor(pid, ref)

Helper function. To monitor a process please use Node’s monitor_process().

add_monitored_by(pid, ref)

Helper function. To monitor a process please use Node’s monitor_process().

monitor_fired(ref)

The monitor ref this process holds fired: it ends here.

Returns:

True if it was active, so the ‘DOWN’ message is delivered

Return type:

bool

remove_monitor(pid, ref)

Helper function. To demonitor a process please use Node’s demonitor_process().

remove_monitored_by(pid, ref)

Helper function. To demonitor a process please use Node’s demonitor_process().