๐๏ธ 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. |
| Your Loop | Logic. You call core.recv_msg(). This is where your business logic lives. |
๐ข Variable. Depends on your code speed. |
| SenderThread | I/O Worker. Pops from Send Queue and calls WhatsApp API. |
๐ข Medium. Handles network latency. |
Why this matters
Because the SenderThread 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: You call
recv_msg(), which blocks until a message is available. - Production: You call
send_msg(), which wraps your text in aMessage$\rightarrow$ Pushes toSend Queue. - Delivery:
SenderThreadwakes up $\rightarrow$ Pops message $\rightarrow$ Calls external API.
๐ 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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
from_payload(payload)
classmethod
Construct a message from a raw transport payload.
Source code in src/loglife/core/messaging.py
32 33 34 35 36 37 38 39 40 41 | |
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 | |