2024-09-01 22:01:07 +08:00
import { _ as s , c as i , o as a , a6 as n } from "./chunks/framework.BcfwU5BI.js" ; const g = JSON . parse ( '{"title":"进程通信","description":"","frontmatter":{"title":"进程通信","order":4},"headers":[],"relativePath":"en/dev/comm.md","filePath":"en/dev/comm.md","lastUpdated":1725102337000}' ) , e = { name : "en/dev/comm.md" } , t = n ( ` <h1 id="channel-communication" tabindex="-1"><strong>Channel Communication</strong> <a class="header-anchor" href="#channel-communication" aria-label="Permalink to "**Channel Communication**""> </a></h1><h3 id="introduction" tabindex="-1">Introduction <a class="header-anchor" href="#introduction" aria-label="Permalink to "Introduction""> </a></h3><p>LiteyukiBot is running in the main process MainProcess, and other plugin framework processes are child processes that come with it. Therefore, it is impossible to communicate through shared memory and direct object transfer. Liteyuki provides a channel <a href="./api/comm/channel.html#class-channel-generic-t"><code>Channel</code></a> for inter-process communication like <code>go</code>. You can send messages to other processes through <a href="./api/comm/channel.html#class-channel-generic-t"><code>Channel</code></a> and listen to messages from other processes.</p><h3 id="example" tabindex="-1">Example <a class="header-anchor" href="#example" aria-label="Permalink to "Example""> </a></h3><p>The channel is full-duplex, with two receiving modes, but only one mode can be used for a channel, that is, passive mode and active mode, passive mode is implemented by the <code>chan.on_receive()</code> decorator callback function, and active mode needs to call <code>chan.receive()</code> to implement</p><ul><li>创建子进程的同时会初始化一个被动通道和一个主动通道,且通道标识为<code>{process_name}-active</code>和<code>{process_name}-passive</code>, </li><li>主进程中通过<code>get_channel</code>函数获取通道对象</li><li>子进程中导入单例<code>active_channel</code>及<code>passive_channel</code>即可</li></ul><blockquote><p>在轻雪插件中(主进程中)</p></blockquote><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> asyncio</span></span>
2024-08-31 19:08:18 +08:00
< span class = "line" > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > from < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > l i t e y u k i . c o m m < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > i m p o r t < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > g e t _ c h a n n e l , C h a n n e l < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > from < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > l i t e y u k i < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > i m p o r t < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > g e t _ b o t < / s p a n > < / s p a n >
< span class = "line" > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > # get _channel函数获取通道对象 , 参数为调用set _channel时的通道标识 < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > channel _passive < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > = < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > g e t _ c h a n n e l ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; n o n e b o t - p a s s i v e & q u o t ; < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > # 获 取 被 动 通 道 < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > channel _active < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > = < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > g e t _ c h a n n e l ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; n o n e b o t - a c t i v e & q u o t ; < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > # 获 取 主 动 通 道 < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > liteyuki _bot < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > = < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > g e t _ b o t ( ) < / s p a n > < / s p a n >
< span class = "line" > < / s p a n >
< span class = "line" > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > # 注册一个函数在轻雪启动后运行 < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > @ liteyuki _bot . on _after _start < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > async < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > d e f < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 F 4 2 C 1 ; - - s h i k i - d a r k : # B 3 9 2 F 0 ; " > s e n d _ d a t a < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ( ) : < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > while < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 0 5 C C 5 ; - - s h i k i - d a r k : # 7 9 B 8 F F ; " > T r u e < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > : < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > channel _passive . send ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; I a m l i t e y u k i m a i n p r o c e s s p a s s i v e & q u o t ; < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > channel _active . send ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; I a m l i t e y u k i m a i n p r o c e s s a c t i v e & q u o t ; < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > await < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > a s y n c i o . s l e e p ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 0 5 C C 5 ; - - s h i k i - d a r k : # 7 9 B 8 F F ; " > 3 < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > # 每 3 秒 发 送 一 次 消 息 < / s p a n > < / s p a n > < / c o d e > < / p r e > < / d i v > < b l o c k q u o t e > < p > 在 子 进 程 中 ( 例 如 N o n e B o t 插 件 中 ) < / p > < / b l o c k q u o t e > < d i v c l a s s = " l a n g u a g e - p y t h o n v p - a d a p t i v e - t h e m e " > < b u t t o n t i t l e = " C o p y C o d e " c l a s s = " c o p y " > < / b u t t o n > < s p a n c l a s s = " l a n g " > p y t h o n < / s p a n > < p r e c l a s s = " s h i k i s h i k i - t h e m e s g i t h u b - l i g h t g i t h u b - d a r k v p - c o d e " t a b i n d e x = " 0 " > < c o d e > < s p a n c l a s s = " l i n e " > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > f r o m < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > n o n e b o t < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > i m p o r t < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > g e t _ d r i v e r < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > from < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > l i t e y u k i . c o m m < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > i m p o r t < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > a c t i v e _ c h a n n e l , p a s s i v e _ c h a n n e l < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > # 子 进 程 中 获 取 通 道 直 接 导 入 进 程 全 局 单 例 即 可 < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > from < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > l i t e y u k i . l o g < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > i m p o r t < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > l o g g e r < / s p a n > < / s p a n >
< span class = "line" > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > driver < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > = < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > g e t _ d r i v e r ( ) < / s p a n > < / s p a n >
< span class = "line" > < / s p a n >
< span class = "line" > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > # 被动模式 , 通过装饰器注册一个函数在接收到消息时运行 , 每次接收到字符串数据时都会运行 < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > @ passive _channel . on _receive < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # E 3 6 2 0 9 ; - - s h i k i - d a r k : # F F A B 7 0 ; " > f i l t e r _ f u n c < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > = l a m b d a < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > d a t a : < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 0 5 C C 5 ; - - s h i k i - d a r k : # 7 9 B 8 F F ; " > i s i n s t a n c e < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ( d a t a , < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 0 5 C C 5 ; - - s h i k i - d a r k : # 7 9 B 8 F F ; " > s t r < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) ) < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > async < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > d e f < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 F 4 2 C 1 ; - - s h i k i - d a r k : # B 3 9 2 F 0 ; " > o n _ p a s s i v e _ r e c e i v e < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ( d a t a ) : < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > logger . info ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > f < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; P a s s i v e r e c e i v e : < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 0 5 C C 5 ; - - s h i k i - d a r k : # 7 9 B 8 F F ; " > { < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > d a t a < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 0 5 C C 5 ; - - s h i k i - d a r k : # 7 9 B 8 F F ; " > } < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) < / s p a n > < / s p a n >
< span class = "line" > < / s p a n >
< span class = "line" > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > # 注册一个函数在NoneBot启动后运行 < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > @ driver . on _startup < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > def < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 F 4 2 C 1 ; - - s h i k i - d a r k : # B 3 9 2 F 0 ; " > o n _ s t a r t u p < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ( ) : < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > while < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 0 5 C C 5 ; - - s h i k i - d a r k : # 7 9 B 8 F F ; " > T r u e < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > : < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > data < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > = < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > a c t i v e _ c h a n n e l . r e c e i v e ( ) < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > logger . info ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > f < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; A c t i v e r e c e i v e : < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 0 5 C C 5 ; - - s h i k i - d a r k : # 7 9 B 8 F F ; " > { < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > d a t a < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 0 5 C C 5 ; - - s h i k i - d a r k : # 7 9 B 8 F F ; " > } < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) < / s p a n > < / s p a n > < / c o d e > < / p r e > < / d i v > < b l o c k q u o t e > < p > 启 动 后 控 制 台 输 出 < / p > < / b l o c k q u o t e > < d i v c l a s s = " l a n g u a g e - l o g v p - a d a p t i v e - t h e m e " > < b u t t o n t i t l e = " C o p y C o d e " c l a s s = " c o p y " > < / b u t t o n > < s p a n c l a s s = " l a n g " > l o g < / s p a n > < p r e c l a s s = " s h i k i s h i k i - t h e m e s g i t h u b - l i g h t g i t h u b - d a r k v p - c o d e " t a b i n d e x = " 0 " > < c o d e > < s p a n c l a s s = " l i n e " > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > 0 0 0 0 - 0 0 - 0 0 < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > 0 0 : 0 0 : 0 0 < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > [ ℹ ️ 信 息 ] P a s s i v e r e c e i v e : I a m l i t e y u k i m a i n p r o c e s s p a s s i v e < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > 0000 - 00 - 00 < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > 0 0 : 0 0 : 0 0 < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > [ ℹ ️ 信 息 ] A c t i v e r e c e i v e : I a m l i t e y u k i m a i n p r o c e s s a c t i v e < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > 0000 - 00 - 00 < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > 0 0 : 0 0 : 0 3 < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > [ ℹ ️ 信 息 ] P a s s i v e r e c e i v e : I a m l i t e y u k i m a i n p r o c e s s p a s s i v e < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > 0000 - 00 - 00 < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > 0 0 : 0 0 : 0 3 < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > [ ℹ ️ 信 息 ] A c t i v e r e c e i v e : I a m l i t e y u k i m a i n p r o c e s s a c t i v e < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ... < / s p a n > < / s p a n > < / c o d e > < / p r e > < / d i v > < h 2 i d = " 共 享 内 存 通 信 " t a b i n d e x = " - 1 " > < s t r o n g > 共 享 内 存 通 信 < / s t r o n g > < a c l a s s = " h e a d e r - a n c h o r " h r e f = " # 共 享 内 存 通 信 " a r i a - l a b e l = " P e r m a l i n k t o & q u o t ; * * 共 享 内 存 通 信 * * & q u o t ; " > < / a > < / h 2 > < h 3 i d = " 简 介 " t a b i n d e x = " - 1 " > 简 介 < a c l a s s = " h e a d e r - a n c h o r " h r e f = " # 简 介 " a r i a - l a b e l = " P e r m a l i n k t o & q u o t ; 简 介 & q u o t ; " > < / a > < / h 3 > < u l > < l i > 相 比 于 普 通 进 程 通 信 , 内 存 共 享 使 得 代 码 编 写 更 加 简 洁 , 轻 雪 框 架 提 供 了 一 个 内 存 共 享 通 信 的 接 口 , 你 可 以 通 过 < c o d e > s t o r a g e < / c o d e > 模 块 实 现 内 存 共 享 通 信 , 该 模 块 封 装 通 道 实 现 < / l i > < l i > 内 存 共 享 是 线 程 安 全 的 , 你 可 以 在 多 个 线 程 中 读 写 共 享 内 存 , 线 程 锁 会 自 动 保 护 共 享 内 存 的 读 写 操 作 < / l i > < / u l > < h 3 i d = " 示 例 " t a b i n d e x = " - 1 " > 示 例 < a c l a s s = " h e a d e r - a n c h o r " h r e f = " # 示 例 " a r i a - l a b e l = " P e r m a l i n k t o & q u o t ; 示 例 & q u o t ; " > < / a > < / h 3 > < b l o c k q u o t e > < p > 在 任 意 进 程 中 均 可 使 用 < / p > < / b l o c k q u o t e > < d i v c l a s s = " l a n g u a g e - p y t h o n v p - a d a p t i v e - t h e m e " > < b u t t o n t i t l e = " C o p y C o d e " c l a s s = " c o p y " > < / b u t t o n > < s p a n c l a s s = " l a n g " > p y t h o n < / s p a n > < p r e c l a s s = " s h i k i s h i k i - t h e m e s g i t h u b - l i g h t g i t h u b - d a r k v p - c o d e " t a b i n d e x = " 0 " > < c o d e > < s p a n c l a s s = " l i n e " > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > f r o m < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > l i t e y u k i . c o m m . s t o r a g e < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > i m p o r t < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > s h a r e d _ m e m o r y < / s p a n > < / s p a n >
< span class = "line" > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > shared _memory . set ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; k e y & q u o t ; < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > , < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; v a l u e & q u o t ; < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > # 设 置 共 享 内 存 < / s p a n > < / s p a n >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > value < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # D 7 3 A 4 9 ; - - s h i k i - d a r k : # F 9 7 5 8 3 ; " > = < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > s h a r e d _ m e m o r y . g e t ( < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 0 3 2 F 6 2 ; - - s h i k i - d a r k : # 9 E C B F F ; " > & q u o t ; k e y & q u o t ; < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 2 4 2 9 2 E ; - - s h i k i - d a r k : # E 1 E 4 E 8 ; " > ) < / s p a n > < s p a n s t y l e = " - - s h i k i - l i g h t : # 6 A 7 3 7 D ; - - s h i k i - d a r k : # 6 A 7 3 7 D ; " > # 获 取 共 享 内 存 < / s p a n > < / s p a n > < / c o d e > < / p r e > < / d i v > < p > 源 代 码 : < a h r e f = " h t t p s : / / g i t h u b . c o m / L i t e y u k i S t u d i o / L i t e y u k i B o t / b l o b / m a i n / l i t e y u k i / c o m m / s t o r a g e . p y " t a r g e t = " _ b l a n k " r e l = " n o r e f e r r e r " > l i t e y u k i / c o m m / s t o r a g e . p y < / a > < / p > ` , 1 9 ) , l = [ t ] ; f u n c t i o n h ( p , k , r , o , d , c ) { r e t u r n a ( ) , i ( " d i v " , n u l l , l ) } c o n s t y = s ( e , [ [ " r e n d e r " , h ] ] ) ; e x p o r t { g a s _ _ p a g e D a t a , y a s d e f a u l t } ;