๐๏ธ Core Architecture
This document explains how the loglife.core framework handles messages, threading, and concurrency efficiently.
๐ System Overview
The Framework is built on a Producer-Consumer architecture using Python's queue.Queue. This ensures that the Main Thread (the Web Server) is never blocked by slow operations like processing logic or making network calls.
๐งต Threading Model
The framework automatically manages the critical background threads for you.
| Thread | Role | Efficiency |
|---|---|---|
| MainThread | Web Server. Receives Webhook & pushes to Receive Queue. Returns 200 OK instantly. |
โก High. Non-blocking. |
| RouterWorker | Logic. Consumes Receive Queue, processes logic (Text/Audio/VCard), and pushes replies to Send Queue. |
๐ข Variable. Depends on your code speed. |
| SenderWorker | I/O Worker. Pops from Send Queue and delegates to the correct Transport. |
๐ข Medium. Handles network latency. |
Why this matters
Because the SenderWorker is separate, your logic loop can queue 10 messages instantly and go back to listening for new inputs, while the Sender Thread handles the slow task of actually delivering them one by one.
๐จ Unified Messaging
The core unifies all inputs (WhatsApp, Emulator, Tests) into a single Message object.
Workflow
- Ingestion: Webhook receives JSON $\rightarrow$ Wraps in
Message$\rightarrow$ Pushes toReceive Queue. - Consumption:
RouterWorkercallsrecv_msg(), which blocks until a message is available. - Production: Your logic calls
send_msg(), which wraps your text in aMessage$\rightarrow$ Pushes toSend Queue. - Delivery:
SenderWorkerwakes up $\rightarrow$ Pops message $\rightarrow$ Callstransports.send_whatsapp_message(or Emulator).
Transport Layer
The system decouples Threading from Protocols.
* Threading: loglife.core.messaging handles queues and workers.
* Transports: loglife.core.transports handles the actual API calls (Requests to WhatsApp, SSE to Emulator).
๐ API Reference
Chat interface core package (transports, clients, shared protocols).
Message
dataclass
Normalized representation of transport messages.
Source code in src/loglife/core/messaging.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
from_payload(payload)
classmethod
Construct a message from a raw transport payload.
Source code in src/loglife/core/messaging.py
44 45 46 47 48 49 50 51 52 53 | |
init()
Initialize the core system (DB, Logging, Workers).
Call this at the start of your application.
Source code in src/loglife/core/interface.py
24 25 26 27 28 29 30 31 32 33 | |
recv_msg(*, block=True, timeout=None)
Receive the next message from the inbound queue.
Blocks until a message is available unless block=False.
Source code in src/loglife/core/interface.py
36 37 38 39 40 41 | |
send_msg(message, to=None)
Send a message to the outbound queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Message | str
|
A Message object OR a string text. |
required |
to
|
str | None
|
The phone number to send to (required if message is a string). |
None
|
Source code in src/loglife/core/interface.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |