prpc.Connection

Connection implementation.

class prpc.connection.Connection(methods=None, loop=None, logger=None, serve_traceback=True, attached_data=None, debug=False)

Single point-to-point RPC connection.

Implements a transport independent protocol.

For server usage, see accept().

For client usage, see connect().

Parameters:
  • methods – Methods to expose through RPC; may be AbstractMethodLocator instance, a valid input for TreeMethodLocator or None.
  • loop – asyncio event loop to use (asyncio.get_eventloop() by default).
  • logger – Logger instance.
  • serve_traceback – Toggle sending full tracebacks on RPC exceptions.
  • attached_data – Custom data (dict or list of pairs) to store in connection ‘data’ property.
  • debug – Adds some excessive logging and checking.

Note

Debug flag enables per-call messages. Without it, Connection will still write out some ‘DEBUG’ level messages, but only for event that are independent on the total number of calls.

async-with call_unary(method, args=None, kwargs=None, timeout=None)

Run unary RPC call.

Parameters:
  • method – Remote method name.
  • args – Positional arguments as a list/tuple.
  • kwargs – Keyword arguments as a dict.
  • timeout – Optional timeout value in seconds (int or float).
Returns:

RpcResponse instance.

async-with call_istream(method, args=None, kwargs=None, timeout=None)

Run istream RPC call (e.g. file download).

Parameters:
  • method – Remote method name.
  • args – Positional arguments as a list/tuple.
  • kwargs – Keyword arguments as a dict.
  • timeout – Optional timeout value in seconds (int or float).
Returns:

RpcResponse instance.

async-with call_ostream(method, args=None, kwargs=None, timeout=None)

Run ostream RPC call (e.g. file upload).

Parameters:
  • method – Remote method name.
  • args – Positional arguments as a list/tuple.
  • kwargs – Keyword arguments as a dict.
  • timeout – Optional timeout value in seconds (int or float).
Returns:

RpcResponse instance.

async-with call_bistream(method, args=None, kwargs=None, timeout=None)

Run bistream RPC call (e.g. subprotocol).

Parameters:
  • method – Remote method name.
  • args – Positional arguments as a list/tuple.
  • kwargs – Keyword arguments as a dict.
  • timeout – Optional timeout value in seconds (int or float).
Returns:

RpcResponse instance.

coroutine call_simple(method, *args, **kwargs)

Run unary RPC call with simplified signature.

Note

For advanced APIs like call cancellation use call_unary()

Parameters:
  • method – Remote method name.
  • *args – Positional arguments (expanded).
  • **kwargs – Keyword arguments (expanded).
Returns:

Call return value.

coroutine connect(socket_factory, handshake_data=None, connect_callback=None, timeout=None)

Attempt to connect to peer in client mode.

Parameters:
  • socket_factory – Async callable returning socket compatible with platform.MessageSocket interface.
  • handshake_data – Custom data to embed into handshake message.
  • connect_callback – Sync or async callable to validate incoming handshake.
  • timeout – Connection timeout (number or None).

Note

If ‘timeout’ is None, connection uses a default value (see protocol.constants). To disable timeout altogether, pass a negative timeout value.

Note

.connect(…) does not support async context manager interface for now. Amount of hacks required to allow both ‘await connection.connect’ and ‘async with connection.connect’ is unpleasant and imposes restrictions on platform modules.

coroutine accept(socket, handshake_data=None, connect_callback=None, timeout=None)

Accept connection in a server mode.

Parameters:
  • socket – Async socket implementing with platform.MessageSocket interface.
  • handshake_data – Custom data to embed into handshake message.
  • connect_callback – Sync or async callable to validate incoming handshake.
  • timeout – Connection timeout (non-negative number or None).

Note

If ‘timeout’ is None, connection uses a default value (see protocol.constants). To disable timeout altogether, pass a negative timeout value.

coroutine close()

Close the connection and interrupt any active calls.

state

Get connection current state.

connected

Check if connection is operational.

active

Check if connection has any active calls.

mode

Get connection mode.

id

Get connection id.

handshake_data

Get handshake data received from peer.

on_close

Signal emitted on close.

locator

Get method locator instance. May be None.

loop

Get the event loop instance used by this connection.

data

Custom dict to store attached data.