import{_ as e,r as t,o,c as p,b as n,d as s,a as c,e as i}from"./app-KOJ2VbPG.js";const l={},u=i(`
轻雪运行在主进程 MainProcess 里,其他插件框架进程是伴随的子进程,因此无法通过内存共享和直接对象传递的方式进行通信,轻雪提供了一个通道Channel
用于跨进程通信,你可以通过Channel
发送消息给其他进程,也可以监听其他进程的消息。
例如子进程接收到用户信息需要重启机器人,这时可以通过通道对主进程发送消息,主进程接收到消息后重启对应子进程。
通道是全双工的,有两种接收模式,但一个通道只能使用一种,即被动模式和主动模式,被动模式由chan.on_receive()
装饰回调函数实现,主动模式需调用chan.receive()
实现
- 创建子进程的同时会初始化一个被动通道和一个主动通道,且通道标识为
{process_name}-active
和{process_name}-passive
, - 主进程中通过
get_channel
函数获取通道对象 - 子进程中导入单例
active_channel
及passive_channel
即可
在轻雪插件中(主进程中)
import asyncio
from liteyuki.comm import get_channel, Channel
from liteyuki import get_bot
channel_passive = get_channel("nonebot-passive")
channel_active = get_channel("nonebot-active")
liteyuki_bot = get_bot()
@liteyuki_bot.on_after_start
async def send_data():
while True:
channel_passive.send("I am liteyuki main process passive")
channel_active.send("I am liteyuki main process active")
await asyncio.sleep(3)
在子进程中(例如NoneBot插件中)
from nonebot import get_driver
from liteyuki.comm import active_channel, passive_channel
from liteyuki.log import logger
driver = get_driver()
@passive_channel.on_receive(filter_func=lambda data: isinstance(data, str))
async def on_passive_receive(data):
logger.info(f"Passive receive: {data}")
@driver.on_startup
def on_startup():
while True:
data = active_channel.receive()
logger.info(f"Active receive: {data}")
启动后控制台输出
0000-00-00 00:00:00 [ℹ️信息] Passive receive: I am liteyuki main process passive
0000-00-00 00:00:00 [ℹ️信息] Active receive: I am liteyuki main process active
0000-00-00 00:00:03 [ℹ️信息] Passive receive: I am liteyuki main process passive
0000-00-00 00:00:03 [ℹ️信息] Active receive: I am liteyuki main process active
...
- 相比于普通进程通信,内存共享使得代码编写更加简洁,轻雪框架提供了一个内存共享通信的接口,你可以通过
storage
模块实现内存共享通信,该模块封装通道实现 - 内存共享是线程安全的,你可以在多个线程中读写共享内存,线程锁会自动保护共享内存的读写操作
在任意进程中均可使用
from liteyuki.comm.storage import shared_memory
shared_memory.set("key", "value")
value = shared_memory.get("key")
`,19),r={href:"https://github.com/LiteyukiStudio/LiteyukiBot/blob/main/liteyuki/comm/storage.py",target:"_blank",rel:"noopener noreferrer"};function d(k,m){const a=t("ExternalLinkIcon");return o(),p("div",null,[u,n("p",null,[s("源代码:"),n("a",r,[s("liteyuki/comm/storage.py"),c(a)])])])}const h=e(l,[["render",d],["__file","dev_comm.html.vue"]]),b=JSON.parse('{"path":"/dev/dev_comm.html","title":"进程通信","lang":"zh-CN","frontmatter":{"title":"进程通信","icon":"exchange-alt","order":4,"category":"开发","description":"通道通信 简介 轻雪运行在主进程 MainProcess 里,其他插件框架进程是伴随的子进程,因此无法通过内存共享和直接对象传递的方式进行通信,轻雪提供了一个通道Channel用于跨进程通信,你可以通过Channel发送消息给其他进程,也可以监听其他进程的消息。 例如子进程接收到用户信息需要重启机器人,这时可以通过通道对主进程发送消息,主进程接收到消息...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/dev_comm.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/dev_comm.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot 轻雪机器人"}],["meta",{"property":"og:title","content":"进程通信"}],["meta",{"property":"og:description","content":"通道通信 简介 轻雪运行在主进程 MainProcess 里,其他插件框架进程是伴随的子进程,因此无法通过内存共享和直接对象传递的方式进行通信,轻雪提供了一个通道Channel用于跨进程通信,你可以通过Channel发送消息给其他进程,也可以监听其他进程的消息。 例如子进程接收到用户信息需要重启机器人,这时可以通过通道对主进程发送消息,主进程接收到消息..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2024-08-16T16:39:49.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-16T16:39:49.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"进程通信\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-16T16:39:49.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":2,"title":"通道通信","slug":"通道通信","link":"#通道通信","children":[{"level":3,"title":"简介","slug":"简介","link":"#简介","children":[]},{"level":3,"title":"示例","slug":"示例","link":"#示例","children":[]}]},{"level":2,"title":"共享内存通信","slug":"共享内存通信","link":"#共享内存通信","children":[{"level":3,"title":"简介","slug":"简介-1","link":"#简介-1","children":[]},{"level":3,"title":"示例","slug":"示例-1","link":"#示例-1","children":[]}]}],"git":{"createdTime":1723413012000,"updatedTime":1723826389000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":4},{"name":"Snowykami","email":"snowykami@outlook.com","commits":1}]},"readingTime":{"minutes":2.52,"words":757},"filePathRelative":"dev/dev_comm.md","localizedDate":"2024年8月11日","autoDesc":true}');export{h as comp,b as data};