### Run Chat Example with Docker Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/channels-chat.md Start the application environment using Docker Compose. ```bash cd examples/channels_chat docker-compose up ``` -------------------------------- ### Run ML Caching Demo Locally Source: https://github.com/benoitc/hornbeam/blob/main/examples/demo/README.md Steps to install dependencies and start the ML caching OTP application. ```bash cd ml_caching # Install Python dependencies pip install -r requirements.txt # Build and run as OTP release rebar3 release _build/default/rel/ml_caching/bin/ml_caching foreground ``` -------------------------------- ### Manual Distributed RPC Setup Source: https://github.com/benoitc/hornbeam/blob/main/examples/demo/README.md Commands to manually start main and worker nodes for distributed RPC. ```bash # Terminal 1 - Main node rebar3 shell --sname main hornbeam:start("app:app", #{ worker_class => asgi, pythonpath => ["examples/demo/distributed_rpc"] }). # Terminal 2 - Worker node rebar3 shell --sname worker net_adm:ping('main@yourhostname'). ``` -------------------------------- ### Run Elixir Example Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/getting-started.md Execute the provided Elixir example script with the necessary library paths. ```bash elixir --erl "-pa _build/default/lib/erlang_python/ebin" examples/elixir_example.exs ``` -------------------------------- ### Run the Application Source: https://github.com/benoitc/hornbeam/blob/main/examples/embedding_chat/README.md Start the Erlang shell or manually start the application. ```bash # Start Erlang shell with application rebar3 shell # Or start manually erl -pa _build/default/lib/*/ebin > application:ensure_all_started(embedding_chat). ``` -------------------------------- ### Install Benchmarking Tools Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/benchmarking.md Commands to install wrk or apache2-utils on macOS and Linux systems. ```bash # macOS brew install wrk # Linux (Ubuntu/Debian) apt-get install wrk # or apt-get install apache2-utils # for ab ``` -------------------------------- ### Configure SSL Server Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/configuration.md Example of starting a Hornbeam server with SSL enabled using certificate and key files. ```erlang hornbeam:start("app:application", #{ bind => "0.0.0.0:443", ssl => true, certfile => "/path/to/cert.pem", keyfile => "/path/to/key.pem" }). ``` -------------------------------- ### Run the Application Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/custom-apps.md Start the rebar3 shell and initialize the application. ```bash rebar3 shell > my_app:start(). ``` -------------------------------- ### Start Hornbeam applications Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/multi-app.md Demonstrates the equivalent methods for starting a single application using either a direct string or a configuration map. ```erlang %% These are equivalent: hornbeam:start("app:application", #{worker_class => wsgi}). hornbeam:start(#{ mounts => [{"/", "app:application", #{worker_class => wsgi}}] }). ``` -------------------------------- ### Start Hornbeam applications Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/configuration.md Examples for starting a single application or multiple applications using the hornbeam:start function. ```erlang %% Single application hornbeam:start("myapp:application", #{ bind => "0.0.0.0:8000", workers => 4, worker_class => asgi }). %% Multiple applications hornbeam:start(#{ mounts => [ {"/api", "api:app", #{worker_class => asgi}}, {"/", "frontend:app", #{worker_class => wsgi}} ], bind => "0.0.0.0:8000" }). ``` -------------------------------- ### Start Chat Example from Erlang Shell Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/channels-chat.md Initialize the application within an Erlang shell session using rebar3. ```erlang rebar3 shell hornbeam:start("examples.channels_chat.app:app", #{ worker_class => asgi, pythonpath => ["examples/channels_chat"], workers => 4 }). ``` -------------------------------- ### Define and start a basic WSGI application Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/wsgi.md A minimal WSGI application implementation and the corresponding command to start it in Hornbeam. ```python # app.py def application(environ, start_response): path = environ.get('PATH_INFO', '/') start_response('200 OK', [ ('Content-Type', 'text/plain'), ]) return [f'You requested: {path}'.encode()] ``` ```erlang hornbeam:start("app:application"). ``` -------------------------------- ### Start multiple applications with hornbeam:start Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/multi-app.md Mount applications by providing a list of tuples containing the URL prefix, the application specification, and configuration options. ```erlang hornbeam:start(#{ mounts => [ {"/api", "api:app", #{worker_class => asgi}}, {"/admin", "admin:app", #{worker_class => wsgi}}, {"/", "frontend:app", #{worker_class => wsgi}} ] }). ``` -------------------------------- ### Start channel application Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/channels.md Launch the application using the CLI or by invoking the Erlang start function with specific configuration parameters. ```bash hornbeam run your_module:app ``` ```erlang hornbeam:start("your_module:app", #{ worker_class => asgi, channel_modules => ["your_module"] }). ``` -------------------------------- ### Build and start Docker container Source: https://github.com/benoitc/hornbeam/blob/main/examples/demo/multi_app/README.md Use Docker Compose to build and launch the multi-app demo environment. ```bash cd examples/demo docker compose up -d multi-app ``` -------------------------------- ### Run Erlang Concurrency Example Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/ai-integration.md Executes the provided Erlang concurrency example script. ```bash # Run the Erlang concurrency example escript examples/erlang_concurrency.erl ``` -------------------------------- ### Start Hornbeam shell Source: https://github.com/benoitc/hornbeam/blob/main/docs/getting-started.md Start the Erlang shell and initialize the Hornbeam application. ```erlang %% Start the shell rebar3 shell %% Start hornbeam with your app hornbeam:start("myapp:application"). ``` -------------------------------- ### Start the Application Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/getting-started.md Initialize the application to start the pool of Python worker processes. ```erlang 1> application:ensure_all_started(erlang_python). {ok, [erlang_python]} ``` -------------------------------- ### Run Chat Example with CLI Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/channels-chat.md Execute the application using the Hornbeam CLI. ```bash hornbeam run examples/channels_chat:app ``` -------------------------------- ### Run Hornbeam Examples with Docker Source: https://github.com/benoitc/hornbeam/blob/main/examples/demo/README.md Commands to build and run the demo services using docker-compose. ```bash # Build and run all examples docker-compose up --build # Or run individual examples docker-compose up ml-caching # http://localhost:9001 docker-compose up distributed-rpc # http://localhost:9002 docker-compose up realtime-chat # http://localhost:9003 ``` -------------------------------- ### Start Web Server Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/distributed-ml.md Connects the web server to GPU nodes and initializes the Hornbeam application. ```erlang # On web-server erl -name web@web-server -setcookie mysecret # Connect to GPU nodes net_adm:ping('worker1@gpu-server-1'). net_adm:ping('worker2@gpu-server-2'). # Start Hornbeam hornbeam:start("app:app", #{ worker_class => asgi, pythonpath => ["distributed_ml"] }). ``` -------------------------------- ### Define and start an ASGI application Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/asgi.md Defines a basic ASGI 3.0 application and demonstrates how to start it using the Hornbeam Erlang interface. ```python # app.py async def application(scope, receive, send): if scope['type'] == 'http': await send({ 'type': 'http.response.start', 'status': 200, 'headers': [[b'content-type', b'text/plain']], }) await send({ 'type': 'http.response.body', 'body': b'Hello from ASGI!', }) ``` ```erlang hornbeam:start("app:application", #{worker_class => asgi}). ``` -------------------------------- ### Configure multi-app deployment Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/configuration.md Example demonstrating complex multi-app configuration with custom routes, specific mount options, and pythonpath settings. ```erlang hornbeam:start(#{ mounts => [ {"/api/v2", "api_v2:app", #{ worker_class => asgi, timeout => 60000 }}, {"/api/v1", "api_v1:app", #{worker_class => wsgi}}, {"/", "frontend:app", #{worker_class => wsgi}} ], routes => [ {"/health", health_handler, #{}}, {"/metrics", metrics_handler, #{}} ], bind => "0.0.0.0:8000", pythonpath => [".", "apps"] }). ``` -------------------------------- ### Install Requirements Source: https://github.com/benoitc/hornbeam/blob/main/examples/embedding_chat/README.md Install the necessary Python dependencies for the embedding model and web framework. ```bash pip install sentence-transformers fastapi ``` -------------------------------- ### Start Hornbeam without options Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/configuration.md Initiate the application using default settings. ```erlang hornbeam:start("app:application"). ``` -------------------------------- ### hornbeam:start/2 Source: https://github.com/benoitc/hornbeam/blob/main/README.md Starts the Hornbeam server with a specified application and configuration options. ```APIDOC ## hornbeam:start(AppSpec, Options) ### Description Starts the server with a WSGI/ASGI application and custom configuration options. ### Parameters - **AppSpec** (string) - The application specification (e.g., "module:application"). - **Options** (map) - Configuration map including bind address, worker settings, and protocol options. ``` -------------------------------- ### Benchmark Output Example Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/asyncio.md Sample output showing expected performance metrics for the event loop. ```text Timer throughput: 150,000 timers/sec Callback dispatch: 200,000 callbacks/sec I/O ready detection: <1ms latency ``` -------------------------------- ### Build and run locally Source: https://github.com/benoitc/hornbeam/blob/main/examples/demo/multi_app/README.md Commands to compile the release using rebar3 and start the application manually. ```bash cd examples/demo/multi_app rebar3 release ``` ```bash _build/default/rel/multi_app/bin/multi_app foreground ``` -------------------------------- ### Run realistic benchmarks with wrk Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/benchmarking.md Start the application and execute load tests against specific endpoints using the wrk tool. ```bash # Start your app hornbeam:start("myapp:app", #{...}). # Benchmark specific endpoints wrk -t4 -c100 -d30s http://localhost:8000/api/users wrk -t4 -c100 -d30s http://localhost:8000/api/search?q=test ``` -------------------------------- ### Start Real-time Chat Demo Source: https://github.com/benoitc/hornbeam/blob/main/examples/demo/README.md Initialize the Hornbeam shell for the real-time chat application. ```bash # Start Hornbeam rebar3 shell hornbeam:start("app:app", #{ worker_class => asgi, pythonpath => ["examples/demo/realtime_chat"] }). ``` -------------------------------- ### Install free-threaded Python Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/scalability.md Commands to install Python 3.13+ with free-threading enabled via Homebrew, source, or pyenv. ```bash # macOS with Homebrew brew install python@3.13 --with-freethreading # Or build from source ./configure --disable-gil make && make install # Or use pyenv PYTHON_CONFIGURE_OPTS="--disable-gil" pyenv install 3.13.0 ``` -------------------------------- ### Start GPU Worker Nodes Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/distributed-ml.md Initializes the Erlang node and starts the erlang_python application on a GPU server. ```bash # On gpu-server-1 erl -name worker1@gpu-server-1 -setcookie mysecret # In the shell application:ensure_all_started(erlang_python). ``` -------------------------------- ### Run Distributed RPC Demo with Docker Source: https://github.com/benoitc/hornbeam/blob/main/examples/demo/README.md Start the 3-node distributed RPC cluster using docker-compose. ```bash cd distributed_rpc docker-compose up --build ``` -------------------------------- ### Configure Hornbeam Source: https://github.com/benoitc/hornbeam/blob/main/docs/getting-started.md Example of common configuration options for Hornbeam. ```erlang hornbeam:start("myapp:application", #{ %% Server binding bind => "0.0.0.0:8000", %% Protocol: wsgi or asgi worker_class => wsgi, %% Number of Python workers workers => 4, %% Request timeout (ms) timeout => 30000, %% Python module search paths pythonpath => [".", "src"] }). ``` -------------------------------- ### Start Hornbeam Chat Application Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/websocket-chat.md Use rebar3 to launch the shell and start the application with the specified worker configuration. ```erlang rebar3 shell hornbeam:start("app:application", #{ worker_class => asgi, pythonpath => ["chat_demo"], workers => 4, websocket_timeout => 300000 % 5 min timeout }). ``` -------------------------------- ### Benchmark Output Example Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/benchmarking.md Sample output showing performance metrics for simple requests, high concurrency, and large response scenarios. ```text === Benchmark: Simple requests (10000 requests, 100 concurrent) === Requests per second: 66000.00 [#/sec] (mean) Time per request: 1.52 [ms] (mean) Failed requests: 0 === Benchmark: High concurrency (5000 requests, 500 concurrent) === Requests per second: 71000.00 [#/sec] (mean) Time per request: 7.04 [ms] (mean) Failed requests: 0 === Benchmark: Large response (1000 requests, 50 concurrent) === Requests per second: 58000.00 [#/sec] (mean) Time per request: 0.86 [ms] (mean) Failed requests: 0 ``` -------------------------------- ### Configure Hornbeam via hornbeam:start/2 Source: https://github.com/benoitc/hornbeam/blob/main/README.md Start the server programmatically using a map of configuration options. ```erlang hornbeam:start("myapp:application", #{ %% Server bind => <<"0.0.0.0:8000">>, ssl => false, certfile => undefined, keyfile => undefined, %% Protocol worker_class => wsgi, % wsgi | asgi http_version => ['HTTP/1.1', 'HTTP/2'], %% Workers workers => 4, timeout => 30000, keepalive => 2, max_requests => 1000, %% ASGI lifespan => auto, % auto | on | off %% WebSocket websocket_timeout => 60000, websocket_max_frame_size => 16777216, % 16MB %% Python pythonpath => [<<".">>] }). ``` -------------------------------- ### Run ASGI application Source: https://github.com/benoitc/hornbeam/blob/main/docs/getting-started.md Define and start an asynchronous ASGI application. ```python # myapp.py async def application(scope, receive, send): await send({ 'type': 'http.response.start', 'status': 200, 'headers': [[b'content-type', b'text/plain']], }) await send({ 'type': 'http.response.body', 'body': b'Hello from ASGI!', }) ``` ```erlang hornbeam:start("myapp:application", #{worker_class => asgi}). ``` -------------------------------- ### Configure Python logging via Python API Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/logging.md Demonstrates manual handler attachment or using the setup helper within Python code. ```python import erlang # ErlangHandler - logging.Handler subclass handler = erlang.ErlangHandler() logging.getLogger().addHandler(handler) # Or use the setup helper erlang.setup_logging(level=20) # INFO level erlang.setup_logging(level=10, format='%(name)s: %(message)s') ``` -------------------------------- ### Start Hornbeam Server Source: https://github.com/benoitc/hornbeam/blob/main/README.md Commands to initialize WSGI or ASGI applications with various configuration options. ```erlang %% Start with a WSGI application hornbeam:start("myapp:application"). %% Start ASGI app (FastAPI, Starlette, etc.) hornbeam:start("main:app", #{worker_class => asgi}). %% With all options hornbeam:start("myapp:application", #{ bind => "0.0.0.0:8000", workers => 4, worker_class => asgi, lifespan => auto }). ``` -------------------------------- ### Run ASGI Hello World Source: https://github.com/benoitc/hornbeam/blob/main/README.md Defines an asynchronous ASGI application and starts it with the asgi worker class. ```python # examples/hello_asgi/app.py async def application(scope, receive, send): await send({ 'type': 'http.response.start', 'status': 200, 'headers': [[b'content-type', b'text/plain']], }) await send({ 'type': 'http.response.body', 'body': b'Hello from Hornbeam ASGI!', }) ``` ```erlang hornbeam:start("app:application", #{ worker_class => asgi, pythonpath => ["examples/hello_asgi"] }). ``` -------------------------------- ### Implement application behaviour Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/erlang-apps.md Define the start and stop callbacks for the Erlang application module. ```erlang %% src/my_app_app.erl -module(my_app_app). -behaviour(application). -export([start/2, stop/1]). start(_StartType, _StartArgs) -> my_app_sup:start_link(). stop(_State) -> ok. ``` -------------------------------- ### Start Django with Hornbeam Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/wsgi.md Launch the Django WSGI application using the Hornbeam server. ```erlang hornbeam:start("myproject.wsgi:application", #{ pythonpath => [".", "myproject"], workers => 4 }). ``` -------------------------------- ### Authentication integration Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/erlang-integration.md Example of verifying sessions across the Erlang-Python boundary. ```erlang %% Erlang side hornbeam:register_function(verify_session, fun([SessionId]) -> case session_store:get(SessionId) of {ok, Session} -> {ok, Session}; not_found -> {error, not_found} end end). ``` ```python # Python side def get_current_user(request): session_id = request.cookies.get('session_id') if not session_id: return None try: session = call('verify_session', session_id) return session.get('user') except: return None ``` -------------------------------- ### Start Hornbeam Application Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/fastapi-app.md Initializes the Hornbeam server with specific worker configurations and Python path settings. ```erlang rebar3 shell hornbeam:start("app:app", #{ worker_class => asgi, lifespan => on, pythonpath => ["fastapi_demo"], workers => 4 }). ``` -------------------------------- ### Quick Start Distributed Tracing Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/logging.md Demonstrates the basic lifecycle of tracing, including enabling, executing Python code with spans, and retrieving results. ```erlang %% Enable tracing ok = py:enable_tracing(). %% Run Python code with spans {ok, _} = py:eval(<<" import erlang with erlang.Span('process-request', user_id=123): do_work() ">>). %% Retrieve collected spans {ok, Spans} = py:get_traces(). %% Spans = [#{name => <<"process-request">>, status => ok, ...}] %% Clean up ok = py:clear_traces(). ok = py:disable_tracing(). ``` -------------------------------- ### Run WSGI Hello World Source: https://github.com/benoitc/hornbeam/blob/main/README.md Defines a standard WSGI application and starts it using the Hornbeam Erlang interface. ```python # examples/hello_wsgi/app.py def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) return [b'Hello from Hornbeam!'] ``` ```erlang hornbeam:start("app:application", #{pythonpath => ["examples/hello_wsgi"]}). ``` -------------------------------- ### Run the Elixir AI integration example Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/ai-integration.md Executes the provided Elixir script with the necessary erlang_python library paths included in the runtime. ```bash # Full Elixir example with AI integration elixir --erl "-pa _build/default/lib/erlang_python/ebin" examples/elixir_example.exs ``` -------------------------------- ### Start Hornbeam Service Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/ml-integration.md Initializes the Hornbeam application with the specified worker configuration. ```erlang hornbeam:start("embedding_service:app", #{ worker_class => asgi, lifespan => on, workers => 4 }). ``` -------------------------------- ### Verify NIF Build Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/scalability.md Checks that the erlang_python application starts and the NIF is correctly loaded. ```erlang %% Check that the NIF loaded successfully 1> application:ensure_all_started(erlang_python). {ok, [erlang_python]} %% Run basic verification 2> py:eval("1 + 1"). {ok, 2} ``` -------------------------------- ### Configure Worker Sizing Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/configuration.md Examples for scaling worker counts based on CPU-bound or I/O-bound workload characteristics. ```erlang %% CPU-bound (ML inference) hornbeam:start("app:app", #{ workers => erlang:system_info(schedulers) % One per CPU }). %% I/O-bound (web app with DB calls) hornbeam:start("app:app", #{ workers => erlang:system_info(schedulers) * 2 }). ``` -------------------------------- ### Retrieve cache statistics Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/python-api.md Example of fetching and printing the current cache hit rate. ```python from hornbeam_ml import cache_stats stats = cache_stats() # {'hits': 150, 'misses': 23, 'hit_rate': 0.867} print(f"Cache hit rate: {stats['hit_rate']:.1%}") ``` -------------------------------- ### Implement a Basic WebSocket Handler Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/websocket.md This example demonstrates an ASGI-compliant WebSocket handler that accepts connections and echoes received text messages back to the client. ```python # app.py async def application(scope, receive, send): if scope['type'] == 'websocket': # Accept the connection await send({'type': 'websocket.accept'}) while True: message = await receive() if message['type'] == 'websocket.disconnect': break if message['type'] == 'websocket.receive': # Echo the message back text = message.get('text', '') await send({ 'type': 'websocket.send', 'text': f'Echo: {text}' }) ``` ```erlang hornbeam:start("app:application", #{worker_class => asgi}). ``` -------------------------------- ### Implement Erlang Application Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/custom-apps.md Define the Erlang module to register functions and start the Hornbeam server. ```erlang %% src/my_app.erl -module(my_app). -export([start/0, stop/0]). start() -> %% Register Erlang functions callable from Python hornbeam:register_function(validate, fun validate/1), hornbeam:register_function(get_config, fun get_config/1), %% Start the server hornbeam:start("app:app", #{ bind => "0.0.0.0:8000", worker_class => asgi, pythonpath => ["priv/python"] }). stop() -> hornbeam:stop(). %% Validation function called from Python validate([Data]) -> case maps:get(<<"id">>, Data, undefined) of undefined -> #{valid => false, errors => [<<"id is required">>]}; Id when is_binary(Id), byte_size(Id) > 0 -> #{valid => true}; _ -> #{valid => false, errors => [<<"id must be a non-empty string">>]} end. %% Config function get_config([Key]) -> application:get_env(my_app, binary_to_atom(Key, utf8), undefined). ``` -------------------------------- ### Run Hornbeam and Gunicorn Source: https://github.com/benoitc/hornbeam/blob/main/README.md Commands to start the application using Gunicorn for standard WSGI or Hornbeam for Erlang-based concurrency. ```bash # With gunicorn (single process, no Erlang features) cd examples/hello_wsgi gunicorn app:application # With hornbeam (Erlang concurrency, shared state, distribution) rebar3 shell > hornbeam:start("app:application", #{pythonpath => ["examples/hello_wsgi"]}). ``` -------------------------------- ### Implement Chat Functionality with Hornbeam JavaScript Client Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/channels-chat.md This example demonstrates connecting to a socket, joining a channel, tracking user presence, and handling real-time messaging events. ```javascript let socket, channel, presence; function joinRoom() { const username = document.getElementById('username').value.trim(); const room = document.getElementById('room').value.trim(); // Create socket connection socket = new Hornbeam.Socket('/ws', { params: { token: 'demo' }, logger: (kind, msg, data) => console.log(`${kind}: ${msg}`, data) }); socket.onOpen(() => updateStatus('Connected')); socket.onClose(() => updateStatus('Disconnected')); socket.onError((error) => console.error('Socket error:', error)); // Create channel channel = socket.channel(`room:${room}`, { username: username }); // Set up presence presence = new Hornbeam.Presence(channel); presence.onJoin((id, current, newPres) => { if (!current) { addMessage('System', `${newPres.metas[0].username} joined`, true); } }); presence.onLeave((id, current, leftPres) => { if (current.metas.length === 0) { addMessage('System', `${leftPres.metas[0].username} left`, true); } }); presence.onSync(() => renderPresences()); // Handle incoming messages channel.on('new_msg', (payload) => { addMessage(payload.username, payload.body); }); channel.on('typing', (payload) => { if (payload.typing) { document.getElementById('typing-indicator').textContent = `${payload.username} is typing...`; } else { document.getElementById('typing-indicator').textContent = ''; } }); // Join the channel channel.join() .receive('ok', (resp) => { console.log('Joined successfully', resp); showChatArea(); }) .receive('error', (resp) => { alert('Failed to join: ' + (resp.reason || 'Unknown error')); }); socket.connect(); } function sendMessage() { const input = document.getElementById('message-input'); const body = input.value.trim(); if (!body) return; channel.push('new_msg', { body: body }) .receive('ok', () => { addMessage(username, body); input.value = ''; }) .receive('error', (resp) => { console.error('Send error', resp); }); } function renderPresences() { const users = presence.list((id, {metas}) => metas[0]); document.getElementById('users').innerHTML = users.map(meta => `${meta.username}` ).join(''); } function leaveRoom() { channel.leave().receive('ok', () => console.log('Left room')); socket.disconnect(); } ``` -------------------------------- ### Run manual benchmarks with wrk Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/benchmarking.md Start the Hornbeam application in the Erlang shell before executing the wrk command in a separate terminal. ```bash # Start hornbeam rebar3 shell # In Erlang shell: hornbeam:start("myapp:application", #{ bind => <<"127.0.0.1:8000">>, worker_class => wsgi }). # In another terminal, run wrk: wrk -t4 -c100 -d30s --latency http://127.0.0.1:8000/ ``` -------------------------------- ### Read Configuration from Environment Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/configuration.md Retrieve environment variables within Erlang to start the application with custom settings. ```erlang %% Read from environment hornbeam:start("app:application", #{ bind => os:getenv("HORNBEAM_BIND", "127.0.0.1:8000"), workers => list_to_integer(os:getenv("HORNBEAM_WORKERS", "4")) }). ``` -------------------------------- ### Initialize Project Structure Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/custom-apps.md Commands to create the base directory and rebar3 project structure. ```bash mkdir my_app && cd my_app rebar3 new app my_app mkdir -p priv/python ``` -------------------------------- ### Initialize a Django project Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/wsgi.md Commands to create and prepare a new Django project for deployment. ```bash django-admin startproject myproject cd myproject python manage.py migrate ``` -------------------------------- ### Configuring Per-Mount Options Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/multi-app.md Define multiple mounts with specific worker classes and timeouts in the Hornbeam start configuration. ```erlang hornbeam:start(#{ mounts => [ %% High-performance async API {"/api", "api:app", #{ worker_class => asgi, timeout => 60000 }}, %% Admin panel {"/admin", "admin:app", #{ worker_class => wsgi, timeout => 30000 }}, %% Static frontend {"/", "frontend:app", #{ worker_class => wsgi }} ], bind => "0.0.0.0:8000" }). ``` -------------------------------- ### Python to Erlang Type Conversion Examples Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/type-conversion.md Demonstrates the evaluation of various Python expressions and their corresponding Erlang return values. ```erlang %% Integers {ok, 42} = py:eval(<<"42">>). {ok, 123456789012345678901234567890} = py:eval(<<"123456789012345678901234567890">>). %% Floats {ok, 3.14} = py:eval(<<"3.14">>). %% Special floats {ok, nan} = py:eval(<<"float('nan')">>). {ok, infinity} = py:eval(<<"float('inf')">>). {ok, neg_infinity} = py:eval(<<"float('-inf')">>). %% Strings {ok, <<"hello">>} = py:eval(<<"'hello'">>). %% Bytes {ok, <<72,101,108,108,111>>} = py:eval(<<"b'Hello'">>). %% Booleans {ok, true} = py:eval(<<"True">>). {ok, false} = py:eval(<<"False">>). %% None {ok, none} = py:eval(<<"None">>). %% Lists {ok, [1, 2, 3]} = py:eval(<<"[1, 2, 3]">>). %% Tuples {ok, {1, 2, 3}} = py:eval(<<"(1, 2, 3)">>). %% Dicts become maps {ok, #{<<"a">> := 1, <<"b">> := 2}} = py:eval(<<"{'a': 1, 'b': 2}">>). ``` -------------------------------- ### Interact with the RAG system in Erlang shell Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/ai-integration.md Example usage of the RAG module to initialize state, add documents, and query the system. ```erlang 1> State0 = rag:init(). 2> State1 = rag:add_document(<<"Erlang was created at Ericsson in 1986.">>, State0). 3> State2 = rag:add_document(<<"The BEAM VM runs Erlang and Elixir code.">>, State1). 4> State3 = rag:add_document(<<"OTP provides behaviors like gen_server.">>, State2). 5> rag:query(<<"When was Erlang created?">>, State3). <<"Erlang was created at Ericsson in 1986.">> ``` -------------------------------- ### Install Free-Threaded Python on macOS via Homebrew Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/testing-free-threading.md Use Homebrew to install a free-threaded version of Python if available in the formula. ```bash # Check if free-threading option is available brew info python@3.13 # If available: brew install python@3.13 --with-freethreading ``` -------------------------------- ### Start Hornbeam with Lifespan Enabled Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/asgi.md Starts the Hornbeam worker with the lifespan option set to 'on' to support startup and shutdown events. ```erlang hornbeam:start("app:app", #{ worker_class => asgi, lifespan => on % Required for lifespan events }). ``` -------------------------------- ### GET /stats Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/embedding-service.md Retrieves statistics about the embedding service. ```APIDOC ## GET /stats ### Description Returns current operational statistics for the service. ### Method GET ### Endpoint /stats ``` -------------------------------- ### Build and Run Production Releases Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/erlang-apps.md Commands to package an application for production and manage its execution as a foreground process or daemon. ```bash # Build release rebar3 as prod release # Run _build/prod/rel/my_app/bin/my_app foreground # Or as daemon _build/prod/rel/my_app/bin/my_app daemon _build/prod/rel/my_app/bin/my_app stop ``` -------------------------------- ### GET /health Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/embedding-service.md Checks the health status of the service. ```APIDOC ## GET /health ### Description Check service health. ### Method GET ### Endpoint /health ### Response #### Success Response (200) - **status** (str) - The health status of the service. - **model_loaded** (bool) - Indicates if the model is currently loaded. ``` -------------------------------- ### erlang.setup_logging Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/logging.md Python-side helper function to configure the logging handler. ```APIDOC ## erlang.setup_logging(level, format) ### Description Configures the Python logging handler to forward logs to Erlang. ### Parameters - **level** (int) - Optional - The logging level number (e.g., 10 for DEBUG, 20 for INFO). - **format** (string) - Optional - A Python format string for log messages. ``` -------------------------------- ### Initialize a new Erlang project Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/erlang-apps.md Use rebar3 to scaffold a new application directory structure. ```bash rebar3 new app my_app cd my_app ``` -------------------------------- ### Get current node name Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/python-api.md Returns the name of the current node. ```python def node() -> str ``` ```python from hornbeam_erlang import node current = node() # 'hornbeam@localhost' ``` -------------------------------- ### Run ML Caching Demo with Docker Source: https://github.com/benoitc/hornbeam/blob/main/examples/demo/README.md Build and run the ML caching demo container from the project root. ```bash # From hornbeam root directory docker build -f examples/demo/Dockerfile.demo \ --build-arg EXAMPLE=ml_caching \ -t ml_caching_demo . docker run -p 8000:8000 ml_caching_demo ``` -------------------------------- ### Install Hornbeam Dependency Source: https://github.com/benoitc/hornbeam/blob/main/README.md Add the Hornbeam dependency to your project's rebar.config file. ```erlang {deps, [ {hornbeam, {git, "https://github.com/benoitc/hornbeam.git", {branch, "main"}}} ]}. ``` -------------------------------- ### Configure Logging and Tracing in Erlang Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/logging.md Initializes the application and demonstrates how to configure logging levels and enable tracing for Python execution. ```erlang %% Basic usage {ok, _} = application:ensure_all_started(erlang_python). %% Logging ok = py:configure_logging(#{level => info}). {ok, _} = py:eval(<<"import logging; logging.info('hello')">>). %% Tracing ok = py:enable_tracing(). {ok, _} = py:eval(<<" import erlang with erlang.Span('work'): pass ">>). {ok, Spans} = py:get_traces(). io:format("Collected ~p spans~n", [length(Spans)]). ``` -------------------------------- ### Feature flags integration Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/erlang-integration.md Example of checking feature flags stored in Erlang from Python. ```erlang %% Erlang side hornbeam:register_function(feature_enabled, fun([Feature, UserId]) -> feature_flags:is_enabled(Feature, UserId) end). ``` ```python # Python side def get_features(user_id): return { 'new_ui': call('feature_enabled', 'new_ui', user_id), 'beta': call('feature_enabled', 'beta', user_id), } ``` -------------------------------- ### GET /metrics Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/distributed-ml.md Retrieves cluster-wide metrics including request counts and node status. ```APIDOC ## GET /metrics ### Description Returns cluster-wide metrics, including total, distributed, and local fallback request counts, as well as the number of active GPU nodes and node selectors. ### Method GET ### Endpoint /metrics ### Response #### Success Response (200) - **requests** (object) - Contains request metrics: total, distributed, and local_fallback. - **cluster** (object) - Contains cluster status: nodes and node_selector_count. #### Response Example { "requests": { "total": 0, "distributed": 0, "local_fallback": 0 }, "cluster": { "nodes": 0, "node_selector_count": 0 } } ``` -------------------------------- ### Execute hooks from Python Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/hooks.md Demonstrates synchronous, asynchronous, and streaming hook execution patterns in Python. ```python from hornbeam_erlang import execute, execute_async, await_result, stream # Synchronous call result = execute('embeddings', 'encode', text) # Call with kwargs result = execute('service', 'process', data, option='fast') # Async call task_id = execute_async('ml', 'train', large_dataset) # ... do other work ... result = await_result(task_id, timeout_ms=60000) # Streaming for chunk in stream('llm', 'generate', prompt): print(chunk, end='', flush=True) ``` -------------------------------- ### Run manual benchmarks with Apache Bench Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/benchmarking.md Execute load tests using ab with specified request counts and concurrency levels. ```bash # Simple benchmark ab -n 10000 -c 100 -k http://127.0.0.1:8000/ # High concurrency ab -n 5000 -c 500 -k http://127.0.0.1:8000/ ``` -------------------------------- ### Compare performance benchmarks Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/testing-free-threading.md Commands to build and run benchmarks for both standard and free-threaded Python configurations. ```bash # Use standard Python export PYTHON_CONFIG=/usr/bin/python3-config rebar3 clean && rebar3 compile escript examples/benchmark.erl --concurrent ``` ```bash # Use free-threaded Python export PYTHON_CONFIG=$HOME/python-nogil/bin/python3-config rebar3 clean && rebar3 compile escript examples/benchmark.erl --concurrent ``` -------------------------------- ### py:tracemalloc_start/0,1 Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/memory.md Starts memory allocation tracing using Python's tracemalloc module. ```APIDOC ## py:tracemalloc_start(FrameDepth) ### Description Starts tracing memory allocations. Optionally specify the number of frames to store per allocation for detailed debugging. ### Parameters - **FrameDepth** (integer) - Optional - Number of frames to store per allocation. ``` -------------------------------- ### Create TCP server with asyncio Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/asyncio.md Uses create_server to listen for incoming TCP connections and EchoServerProtocol to echo received data back to the client. ```python import asyncio class EchoServerProtocol(asyncio.Protocol): def connection_made(self, transport): peername = transport.get_extra_info('peername') print(f'Connection from {peername}') self.transport = transport def data_received(self, data): message = data.decode() print(f'Received: {message}') # Echo back self.transport.write(data) def connection_lost(self, exc): print('Connection closed') async def main(): loop = asyncio.get_running_loop() server = await loop.create_server( EchoServerProtocol, host='127.0.0.1', port=8888, reuse_address=True ) async with server: await server.serve_forever() ``` -------------------------------- ### Perform cached inference Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/python-api.md Examples of running inference with automatic caching and custom cache keys. ```python from hornbeam_ml import cached_inference # Embeddings are cached by input hash embedding = cached_inference(model.encode, text) # Same text returns cached result instantly embedding2 = cached_inference(model.encode, text) # Cache hit! # Custom cache key embedding = cached_inference( model.encode, text, cache_key=f"embed:v2:{doc_id}" ) ``` -------------------------------- ### Retrieve application metrics Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/fastapi-app.md Exposes internal state and cache statistics via a GET endpoint. ```python @app.get("/metrics") async def get_metrics(): from hornbeam_ml import cache_stats return { 'requests': { 'total': state_get('metrics:requests') or 0, 'status_200': state_get('metrics:status:200') or 0, 'status_404': state_get('metrics:status:404') or 0, }, 'items': { 'count': len([k for k in state_keys('item:') if ':' not in k[5:]]), }, 'websocket': { 'connections': state_get('ws:connections') or 0, }, 'ml_cache': cache_stats() } ``` -------------------------------- ### Create and Activate AI Virtual Environment Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/ai-integration.md Commands to set up a Python virtual environment with necessary AI dependencies and activate it within an Erlang shell. ```bash # Create venv with AI dependencies python3 -m venv ai_venv source ai_venv/bin/activate # Install common AI libraries pip install sentence-transformers numpy openai anthropic ``` ```erlang 1> application:ensure_all_started(erlang_python). {ok, [erlang_python]} 2> py:activate_venv(<<"/path/to/ai_venv">>). ok ``` -------------------------------- ### Build the Application Source: https://github.com/benoitc/hornbeam/blob/main/examples/embedding_chat/README.md Compile the Erlang project using rebar3. ```bash cd examples/embedding_chat rebar3 compile ``` -------------------------------- ### Establish TCP client connection with asyncio Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/asyncio.md Uses create_connection to initiate a TCP client and EchoClientProtocol to handle data transmission and connection lifecycle. ```python import asyncio class EchoClientProtocol(asyncio.Protocol): def __init__(self, message, on_con_lost): self.message = message self.on_con_lost = on_con_lost def connection_made(self, transport): transport.write(self.message.encode()) def data_received(self, data): print(f'Received: {data.decode()}') def connection_lost(self, exc): self.on_con_lost.set_result(True) async def main(): loop = asyncio.get_running_loop() on_con_lost = loop.create_future() transport, protocol = await loop.create_connection( lambda: EchoClientProtocol('Hello, World!', on_con_lost), host='127.0.0.1', port=8888 ) try: await on_con_lost finally: transport.close() ``` -------------------------------- ### Execute hooks from Erlang Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/hooks.md Demonstrates synchronous, asynchronous, and streaming hook execution patterns in Erlang. ```erlang %% Synchronous {ok, Embedding} = hornbeam_hooks:execute(<<"embeddings">>, <<"encode">>, [Text], #{}). %% Async {ok, TaskId} = hornbeam_hooks:execute_async(<<"ml">>, <<"train">>, [Data], #{}), %% ... do other work ... {ok, Result} = hornbeam_hooks:await_result(TaskId, 60000). %% Streaming {ok, Gen} = hornbeam_hooks:stream(<<"llm">>, <<"generate">>, [Prompt], #{}), stream_loop(Gen). stream_loop(Gen) -> case Gen() of {value, Chunk} -> io:format("~s", [Chunk]), stream_loop(Gen); done -> ok end. ``` -------------------------------- ### Configure via Environment Variables Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/configuration.md Set configuration values using shell environment variables. ```bash # Set via environment export HORNBEAM_BIND="0.0.0.0:8000" export HORNBEAM_WORKERS=8 export HORNBEAM_TIMEOUT=30000 ``` -------------------------------- ### Run Event Loop Benchmarks Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/asyncio.md Execute the provided Python script to measure timer throughput, callback dispatch, and I/O latency. ```bash python3 examples/benchmark_event_loop.py ``` -------------------------------- ### Start Hornbeam Service Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/embedding-service.md Initiate the service within a rebar3 shell using the specified configuration map. ```erlang rebar3 shell hornbeam:start("app:app", #{ worker_class => asgi, lifespan => on, pythonpath => ["embedding_service"], workers => 4, timeout => 60000 }). ``` -------------------------------- ### Configure CI benchmarking workflow Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/benchmarking.md Automate performance testing in GitHub Actions by setting up the environment, compiling the project, and running benchmark scripts. ```yaml # .github/workflows/benchmark.yml name: Benchmark on: pull_request: branches: [main] jobs: benchmark: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Erlang uses: erlef/setup-beam@v1 with: otp-version: '27.1' - name: Setup Python uses: actions/setup-python@v5 with: python-version: '3.13' - name: Install wrk run: sudo apt-get install -y wrk - name: Compile run: rebar3 compile - name: Run benchmarks run: python benchmarks/run_benchmark.py --output results.json - name: Upload results uses: actions/upload-artifact@v4 with: name: benchmark-results path: results.json ``` -------------------------------- ### Get multiple keys from ETS Source: https://github.com/benoitc/hornbeam/blob/main/docs/reference/python-api.md Retrieves multiple values from the shared ETS state in a single operation. ```python def state_get_multi(keys: list) -> dict ``` ```python from hornbeam_erlang import state_get_multi users = state_get_multi(['user:1', 'user:2', 'user:3']) # {'user:1': {...}, 'user:2': {...}, 'user:3': None} ``` -------------------------------- ### Build erlang_python for free-threading Source: https://github.com/benoitc/hornbeam/blob/main/docs/erlang-python/testing-free-threading.md Configures the build environment by pointing to the appropriate python3-config path. ```bash # Clean previous builds rebar3 clean # Set Python config path (adjust for your installation) export PYTHON_CONFIG=$HOME/python-nogil/bin/python3-config # Or if using pyenv export PYTHON_CONFIG=$(pyenv prefix 3.13.0)/bin/python3-config # Build rebar3 compile ``` -------------------------------- ### GET /stats Source: https://github.com/benoitc/hornbeam/blob/main/docs/examples/embedding-service.md Retrieves service statistics including document count, request metrics, and cache performance. ```APIDOC ## GET /stats ### Description Get service statistics. ### Method GET ### Endpoint /stats ### Response #### Success Response (200) - **model** (str) - The name of the loaded model. - **index** (object) - Statistics regarding indexed documents. - **requests** (object) - Count of various request types. - **cache** (object) - Cache hit/miss statistics. ``` -------------------------------- ### Application Configuration Source: https://github.com/benoitc/hornbeam/blob/main/examples/embedding_chat/README.md Define the model name, port, and bind address in the application source file. ```erlang {env, [ {model_name, "all-MiniLM-L6-v2"}, % sentence-transformers model {port, 8000}, % HTTP port {bind, "0.0.0.0"} % Bind address ]} ``` -------------------------------- ### Test WebSockets with websocat Source: https://github.com/benoitc/hornbeam/blob/main/docs/guides/websocket.md Use the websocat command-line tool to connect to a running WebSocket server. ```bash websocat ws://localhost:8000/ws ``` -------------------------------- ### Compile project Source: https://github.com/benoitc/hornbeam/blob/main/docs/getting-started.md Compile the project using rebar3. ```bash rebar3 compile ```