mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2024-11-13 14:27:26 +08:00
📝 添加shared_memory文档
This commit is contained in:
parent
b356524a9e
commit
c3914b2b15
@ -13,71 +13,68 @@ category: 开发
|
|||||||
|
|
||||||
例如子进程接收到用户信息需要重启机器人,这时可以通过通道对主进程发送消息,主进程接收到消息后重启对应子进程。
|
例如子进程接收到用户信息需要重启机器人,这时可以通过通道对主进程发送消息,主进程接收到消息后重启对应子进程。
|
||||||
|
|
||||||
### 快速开始
|
### 示例
|
||||||
|
|
||||||
通道是全双工的,有两种接收模式,但一个通道只能使用一种,即被动模式和主动模式,被动模式由`chan.on_receive()`装饰回调函数实现,主动模式需调用`chan.receive()`实现
|
通道是全双工的,有两种接收模式,但一个通道只能使用一种,即被动模式和主动模式,被动模式由`chan.on_receive()`装饰回调函数实现,主动模式需调用`chan.receive()`实现
|
||||||
|
|
||||||
轻雪核心在创建子进程的同时会初始化两个通道,一个被动通道和一个主动通道,且通道标识为`{process_name}-active`和`{process_name}-passive`,你可以通过`get_channel`函数获取通道对象,然后通过`send`方法发送消息
|
- 创建子进程的同时会初始化一个被动通道和一个主动通道,且通道标识为`{process_name}-active`和`{process_name}-passive`,
|
||||||
|
- 主进程中通过`get_channel`函数获取通道对象
|
||||||
|
- 子进程中导入单例`active_channel`及`passive_channel`即可
|
||||||
|
|
||||||
- 在轻雪插件中
|
> 在轻雪插件中(主进程中)
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from liteyuki.comm import get_channel, Channel
|
from liteyuki.comm import get_channel, Channel
|
||||||
from liteyuki import get_bot
|
from liteyuki import get_bot
|
||||||
|
|
||||||
# get_channel函数获取通道对象,参数为调用set_channel时的通道标识
|
# get_channel函数获取通道对象,参数为调用set_channel时的通道标识
|
||||||
# 每个进程只能获取在当前进程通过set_channel设置的通道
|
|
||||||
# 轻雪已经在创建进程时把每个通道都传递给了主进程和子进程,以便在哪个进程都可以被获取
|
|
||||||
channel_passive = get_channel("nonebot-passive") # 获取被动通道
|
channel_passive = get_channel("nonebot-passive") # 获取被动通道
|
||||||
channel_active = get_channel("nonebot-active") # 获取主动通道
|
channel_active = get_channel("nonebot-active") # 获取主动通道
|
||||||
liteyuki_bot = get_bot()
|
liteyuki_bot = get_bot()
|
||||||
|
|
||||||
|
|
||||||
# 注册一个函数在轻雪启动后运行
|
# 注册一个函数在轻雪启动后运行
|
||||||
@liteyuki_bot.on_after_start
|
@liteyuki_bot.on_after_start
|
||||||
async def do_something_after_start():
|
async def send_data():
|
||||||
while True:
|
while True:
|
||||||
channel_passive.send("I am liteyuki main process passive")
|
channel_passive.send("I am liteyuki main process passive")
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
channel_active.send("I am liteyuki main process active")
|
channel_active.send("I am liteyuki main process active")
|
||||||
await asyncio.sleep(3)
|
await asyncio.sleep(3) # 每3秒发送一次消息
|
||||||
```
|
```
|
||||||
|
|
||||||
- 在子进程中(可以是NoneBot插件中)
|
> 在子进程中(例如NoneBot插件中)
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from nonebot import get_driver
|
from nonebot import get_driver
|
||||||
from liteyuki.comm import get_channel
|
from liteyuki.comm import active_channel, passive_channel # 子进程中获取通道直接导入进程全局单例即可
|
||||||
from liteyuki.log import logger
|
from liteyuki.log import logger
|
||||||
|
|
||||||
driver = get_driver()
|
driver = get_driver()
|
||||||
|
|
||||||
# 通过get_channel函数获取通道对象,参数为调用set_channel时的通道标识
|
|
||||||
channel_passive = get_channel("nonebot-passive")
|
|
||||||
channel_active = get_channel("nonebot-active")
|
|
||||||
|
|
||||||
# 被动模式,通过装饰器注册一个函数在接收到消息时运行,每次接收到字符串数据时都会运行
|
# 被动模式,通过装饰器注册一个函数在接收到消息时运行,每次接收到字符串数据时都会运行
|
||||||
@channel_passive.on_receive(filter_func=lambda data: isinstance(data, str))
|
@passive_channel.on_receive(filter_func=lambda data: isinstance(data, str))
|
||||||
async def on_passive_receive(data):
|
async def on_passive_receive(data):
|
||||||
logger.info(f"Passive receive: {data}")
|
logger.info(f"Passive receive: {data}")
|
||||||
|
|
||||||
|
|
||||||
# 注册一个函数在NoneBot启动后运行
|
# 注册一个函数在NoneBot启动后运行
|
||||||
@driver.on_startup
|
@driver.on_startup
|
||||||
async def on_startup():
|
def on_startup():
|
||||||
while True:
|
while True:
|
||||||
data = channel_active.receive()
|
data = active_channel.receive()
|
||||||
logger.info(f"Active receive: {data}")
|
logger.info(f"Active receive: {data}")
|
||||||
```
|
```
|
||||||
|
|
||||||
- 启动后控制台输出
|
> 启动后控制台输出
|
||||||
|
|
||||||
```shell
|
```log
|
||||||
..-..-.. ..:..:.. [ℹ️信息] Passive receive: I am liteyuki main process passive
|
0000-00-00 00:00:00 [ℹ️信息] Passive receive: I am liteyuki main process passive
|
||||||
..-..-.. ..:..:.. [ℹ️信息] Active receive: I am liteyuki main process active
|
0000-00-00 00:00:00 [ℹ️信息] Active receive: I am liteyuki main process active
|
||||||
..-..-.. ..:..:.. [ℹ️信息] Passive receive: I am liteyuki main process passive
|
0000-00-00 00:00:03 [ℹ️信息] Passive receive: I am liteyuki main process passive
|
||||||
..-..-.. ..:..:.. [ℹ️信息] Active receive: I am liteyuki main process active
|
0000-00-00 00:00:03 [ℹ️信息] Active receive: I am liteyuki main process active
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -85,12 +82,12 @@ async def on_startup():
|
|||||||
|
|
||||||
### 简介
|
### 简介
|
||||||
|
|
||||||
- 相比于普通进程通信,内存共享使得代码编写更加简洁,轻雪框架提供了一个内存共享通信的接口,你可以通过`storage`模块实现内存共享通信
|
- 相比于普通进程通信,内存共享使得代码编写更加简洁,轻雪框架提供了一个内存共享通信的接口,你可以通过`storage`模块实现内存共享通信,该模块封装通道实现
|
||||||
- 内存共享是线程安全的,你可以在多个线程中读写共享内存,线程锁会自动保护共享内存的读写操作
|
- 内存共享是线程安全的,你可以在多个线程中读写共享内存,线程锁会自动保护共享内存的读写操作
|
||||||
|
|
||||||
### 快速开始
|
### 示例
|
||||||
|
|
||||||
- 在任意进程中均可使用
|
> 在任意进程中均可使用
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from liteyuki.comm.storage import shared_memory
|
from liteyuki.comm.storage import shared_memory
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
---
|
|
||||||
title: 内存共享通信
|
|
||||||
icon: exchange-alt
|
|
||||||
order: 5
|
|
||||||
category: 开发
|
|
||||||
---
|
|
||||||
|
|
||||||
# 简介
|
|
||||||
|
|
||||||
相比于普通进程通信,内存共享使得代码编写更加简洁,轻雪框架提供了一个内存共享通信的接口,你可以通过`shared_memory`模块实现内存共享通信
|
|
||||||
内存共享是线程安全的,你可以在多个线程中读写共享内存,线程锁会自动保护共享内存的读写操作
|
|
||||||
|
|
||||||
## 快速开始
|
|
||||||
|
|
||||||
- 在任意进程中均可使用
|
|
||||||
|
|
||||||
```python
|
|
||||||
from liteyuki.comm.storage import shared_memory
|
|
||||||
|
|
||||||
shared_memory.set("key", "value") # 设置共享内存
|
|
||||||
value = shared_memory.get("key") # 获取共享内存
|
|
||||||
```
|
|
||||||
|
|
||||||
- 源代码:[liteyuki/comm/storage.py](https://github.com/LiteyukiStudio/LiteyukiBot/blob/main/liteyuki/comm/storage.py)
|
|
Loading…
Reference in New Issue
Block a user