py_buffer (erlang_python v2.3.1)
View SourceZero-copy WSGI input buffer for streaming HTTP bodies.
This module provides a buffer that can be written by Erlang and read by Python with zero-copy semantics. The buffer is suitable for use as wsgi.input in WSGI applications.
Usage
%% Create a buffer (chunked encoding - unknown size)
{ok, Buf} = py_buffer:new(),
%% Or with known content length
{ok, Buf} = py_buffer:new(1024),
%% Write HTTP body chunks
ok = py_buffer:write(Buf, <<"chunk1">>),
ok = py_buffer:write(Buf, <<"chunk2">>),
%% Signal end of data
ok = py_buffer:close(Buf),
%% Pass to Python WSGI - buffer is automatically converted
py_context:call(Ctx, <<"myapp">>, <<"handle">>,
[#{<<"wsgi.input">> => Buf}], #{}).On the Python side, the buffer provides a file-like interface:
python
def handle(environ):
body = environ['wsgi.input'].read() # Blocks until data ready
# Or use readline(), readlines(), iteration
for line in environ['wsgi.input']:
process(line)
Summary
Functions
Close the buffer (signal end of data).
Create a new buffer for chunked/streaming data.
Create a new buffer with known content length.
Write data to the buffer.
Functions
-spec close(reference()) -> ok.
Close the buffer (signal end of data).
Sets the EOF flag and wakes up any Python threads waiting for data. After calling close, no more data can be written, and Python's read() will return any remaining buffered data followed by empty bytes.
Create a new buffer for chunked/streaming data.
Use this when the content length is unknown (chunked transfer encoding). The buffer will grow as needed.
-spec new(non_neg_integer() | undefined) -> {ok, reference()} | {error, term()}.
Create a new buffer with known content length.
Pre-allocates the buffer to the specified size for better performance.
Write data to the buffer.
Appends data to the buffer and signals any waiting Python readers. This function is safe to call from multiple processes, but typically only one process should write to a buffer.