Skip to content

Commit 98f8066

Browse files
committed
Add erlang_asyncio architecture diagram to asyncio.md
1 parent caa5405 commit 98f8066

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

docs/asyncio.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,47 @@ The `erlang_asyncio` module provides asyncio-compatible primitives that use Erla
550550

551551
Unlike the standard `asyncio` module which uses Python's polling-based event loop, `erlang_asyncio` uses Erlang's `erlang:send_after/3` for timers and integrates directly with the BEAM scheduler. This eliminates Python event loop overhead (~0.5-1ms per operation) and provides more precise timing.
552552

553+
### Architecture
554+
555+
```
556+
┌─────────────────────────────────────────────────────────────────────────┐
557+
│ erlang_asyncio.sleep() │
558+
│ │
559+
│ Python Erlang │
560+
│ ────── ────── │
561+
│ │
562+
│ ┌─────────────────┐ ┌─────────────────────────────────┐ │
563+
│ │ erlang_asyncio │ │ py_event_worker │ │
564+
│ │ .sleep(0.1) │ │ │ │
565+
│ └────────┬────────┘ │ handle_info({sleep_wait,...}) │ │
566+
│ │ │ │ │ │
567+
│ ▼ │ ▼ │ │
568+
│ ┌─────────────────┐ │ erlang:send_after(100ms) │ │
569+
│ │ py_event_loop. │──{sleep_wait,│ │ │ │
570+
│ │ _erlang_sleep() │ 100, Id}──▶│ ▼ │ │
571+
│ └────────┬────────┘ │ handle_info({sleep_complete}) │ │
572+
│ │ │ │ │ │
573+
│ ┌────────▼────────┐ │ ▼ │ │
574+
│ │ Release GIL │ │ py_nif:dispatch_sleep_complete │ │
575+
│ │ pthread_cond_ │◀─────────────│ │ │ │
576+
│ │ wait() │ signal └─────────┼───────────────────────┘ │
577+
│ └────────┬────────┘ │ │
578+
│ │ │ │
579+
│ ▼ ▼ │
580+
│ ┌─────────────────┐ ┌─────────────────────────────────┐ │
581+
│ │ Reacquire GIL │ │ pthread_cond_broadcast() │ │
582+
│ │ Return result │ │ (wakes Python thread) │ │
583+
│ └─────────────────┘ └─────────────────────────────────┘ │
584+
│ │
585+
└─────────────────────────────────────────────────────────────────────────┘
586+
```
587+
588+
**Key features:**
589+
- **GIL released during sleep** - Python thread doesn't hold the GIL while waiting
590+
- **BEAM scheduler integration** - Uses Erlang's native timer system
591+
- **Zero CPU usage** - Condition variable wait, no polling
592+
- **Sub-millisecond precision** - Timers managed by BEAM scheduler
593+
553594
### Basic Usage
554595

555596
```python

0 commit comments

Comments
 (0)