- Replace open/write/flush/close per log call with persistent file handle - Use threading.Queue + background daemon thread for non-blocking writes - Only flush on exception/critical levels or periodically (every 1s idle) - Queue full protection: drop oldest entry instead of blocking event loop - Eliminates disk I/O blocking on slow storage (NFS/cloud disk) during high traffic
59 lines
693 B
Markdown
59 lines
693 B
Markdown
# EventDispatcher
|
|
|
|
生产级异步事件调度器。
|
|
|
|
## 特性
|
|
|
|
- 支持普通函数
|
|
- 支持 async 协程
|
|
- 支持实例方法
|
|
- 弱引用自动GC
|
|
- 异常隔离
|
|
- 超时控制
|
|
- 自定义错误处理
|
|
|
|
---
|
|
|
|
# 使用示例
|
|
|
|
```python
|
|
import asyncio
|
|
|
|
from event_dispatcher import EventDispatcher
|
|
|
|
|
|
def on_login(data):
|
|
print(data)
|
|
|
|
|
|
async def main():
|
|
|
|
dispatcher = EventDispatcher()
|
|
|
|
dispatcher.bind(
|
|
"login",
|
|
on_login
|
|
)
|
|
|
|
await dispatcher.dispatch(
|
|
"login",
|
|
{
|
|
"user": "张三"
|
|
}
|
|
)
|
|
|
|
|
|
asyncio.run(main())
|
|
```
|
|
|
|
---
|
|
|
|
# 项目结构
|
|
|
|
```text
|
|
event_dispatcher_project/
|
|
├── event_dispatcher.py
|
|
└── README.md
|
|
```
|
|
|