Skip to content

Commit 96dd12b

Browse files
committed
Update channel docs to show sender PID pattern
1 parent 1419ddd commit 96dd12b

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

docs/channel.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ Use channels when you need:
2626
%% Create a channel
2727
{ok, Ch} = py_channel:new(),
2828

29-
%% Send messages to Python
30-
ok = py_channel:send(Ch, {request, <<"data">>}),
31-
ok = py_channel:send(Ch, {request, <<"more">>}),
29+
%% Send messages with sender PID for replies
30+
ok = py_channel:send(Ch, {request, self(), <<"data">>}),
31+
32+
%% Wait for response
33+
receive
34+
{response, Result} ->
35+
io:format("Got result: ~p~n", [Result])
36+
end,
3237

3338
%% Close when done
3439
py_channel:close(Ch).
@@ -37,27 +42,35 @@ py_channel:close(Ch).
3742
### Python Side (Sync)
3843

3944
```python
40-
from erlang.channel import Channel
45+
from erlang.channel import Channel, reply
4146

4247
def process_messages(channel_ref):
4348
ch = Channel(channel_ref)
4449

45-
# Iterate over messages until closed
4650
for msg in ch:
47-
print(f"Received: {msg}")
51+
# Extract sender PID from message
52+
_, sender_pid, data = msg
53+
54+
# Process and reply
55+
result = process(data)
56+
reply(sender_pid, ('response', result))
4857
```
4958

5059
### Python Side (Async)
5160

5261
```python
53-
from erlang.channel import Channel
62+
from erlang.channel import Channel, reply
5463

5564
async def process_messages(channel_ref):
5665
ch = Channel(channel_ref)
5766

58-
# Async iteration - yields to other coroutines while waiting
5967
async for msg in ch:
60-
print(f"Received: {msg}")
68+
# Extract sender PID from message
69+
_, sender_pid, data = msg
70+
71+
# Process and reply
72+
result = await process(data)
73+
reply(sender_pid, ('response', result))
6174
```
6275

6376
## Erlang API

0 commit comments

Comments
 (0)