py_channel (erlang_python v2.3.1)

View Source

Bidirectional channel for Erlang-Python communication.

Channels provide efficient streaming message passing between Erlang processes and Python code without syscall overhead.

Usage

   %% Create a channel
   {ok, Ch} = py_channel:new(),
  
   %% Send messages to Python
   ok = py_channel:send(Ch, {request, self(), <<"data">>}),
  
   %% Python: ch = Channel(ref); msg = ch.receive()
   %% Python: reply(pid, term)
  
   %% Close when done
   py_channel:close(Ch).

Backpressure

Channels support backpressure via max_size option:

   {ok, Ch} = py_channel:new(#{max_size => 10000}),
   %% Returns 'busy' when queue exceeds max_size
   case py_channel:send(Ch, LargeData) of
       ok -> proceed;
       busy -> wait_and_retry
   end.

Summary

Functions

Close a channel.

Get channel information.

Create a new channel with default settings.

Create a new channel with options.

Register channel callback handlers for Python.

Send a message to a channel.

Types

channel/0

-type channel() :: reference().

opts/0

-type opts() :: #{max_size => non_neg_integer()}.

Functions

close(Channel)

-spec close(channel()) -> ok.

Close a channel.

Signals Python receivers that no more messages will arrive. Any blocked receive() calls will raise StopIteration.

info(Channel)

-spec info(channel()) -> map().

Get channel information.

Returns a map with:

  • size - Current queue size in bytes
  • max_size - Maximum queue size (0 = unlimited)
  • closed - Whether the channel is closed

new()

-spec new() -> {ok, channel()} | {error, term()}.

Create a new channel with default settings.

Creates an unbounded channel for message passing.

new(Opts)

-spec new(opts()) -> {ok, channel()} | {error, term()}.

Create a new channel with options.

Options:

  • max_size - Maximum queue size in bytes for backpressure (0 = unlimited)

register_callbacks()

-spec register_callbacks() -> ok.

Register channel callback handlers for Python.

This should be called during application startup to enable Python's erlang.call('_py_channel_receive', ...) etc.

send(Channel, Term)

-spec send(channel(), term()) -> ok | busy | {error, term()}.

Send a message to a channel.

The term is serialized and queued for Python to receive. If the queue exceeds max_size, returns busy (backpressure).