py_channel (erlang_python v2.3.1)
View SourceBidirectional 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
-type channel() :: reference().
-type opts() :: #{max_size => non_neg_integer()}.
Functions
-spec close(channel()) -> ok.
Close a channel.
Signals Python receivers that no more messages will arrive. Any blocked receive() calls will raise StopIteration.
Get channel information.
Returns a map with:
size- Current queue size in bytesmax_size- Maximum queue size (0 = unlimited)closed- Whether the channel is closed
Create a new channel with default settings.
Creates an unbounded channel for message passing.
Create a new channel with options.
Options:
max_size- Maximum queue size in bytes for backpressure (0 = unlimited)
-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 a message to a channel.
The term is serialized and queued for Python to receive. If the queue exceeds max_size, returns busy (backpressure).