import{_ as n,o as a,c as s,e}from"./app-Ct8dV_oN.js";const t={},c=e(`

def set_channel(name: str, channel: Channel) -> None

设置通道实例

Args:

name: 通道名称

channel: 通道实例
源代码
def set_channel(name: str, channel: Channel):
    """
    设置通道实例
    Args:
        name: 通道名称
        channel: 通道实例
    """
    if not isinstance(channel, Channel):
        raise TypeError(f'channel_ must be an instance of Channel, {type(channel)} found')
    if IS_MAIN_PROCESS:
        _channel[name] = channel
    else:
        channel_deliver_passive_channel.send(('set_channel', {'name': name, 'channel_': channel}))

def set_channels(channels: dict[str, Channel]) -> None

设置通道实例

Args:

channels: 通道名称
源代码
def set_channels(channels: dict[str, Channel]):
    """
    设置通道实例
    Args:
        channels: 通道名称
    """
    for (name, channel) in channels.items():
        set_channel(name, channel)

def get_channel(name: str) -> Channel

获取通道实例

Args:

name: 通道名称

Returns:

源代码
def get_channel(name: str) -> Channel:
    """
    获取通道实例
    Args:
        name: 通道名称
    Returns:
    """
    if IS_MAIN_PROCESS:
        return _channel[name]
    else:
        recv_chan = Channel[Channel[Any]]('recv_chan')
        channel_deliver_passive_channel.send(('get_channel', {'name': name, 'recv_chan': recv_chan}))
        return recv_chan.receive()

def get_channels() -> dict[str, Channel]

获取通道实例

Returns:

源代码
def get_channels() -> dict[str, Channel]:
    """
    获取通道实例
    Returns:
    """
    if IS_MAIN_PROCESS:
        return _channel
    else:
        recv_chan = Channel[dict[str, Channel[Any]]]('recv_chan')
        channel_deliver_passive_channel.send(('get_channels', {'recv_chan': recv_chan}))
        return recv_chan.receive()

def on_set_channel(data: tuple[str, dict[str, Any]]) -> None

源代码
@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == 'set_channel')
def on_set_channel(data: tuple[str, dict[str, Any]]):
    (name, channel) = (data[1]['name'], data[1]['channel_'])
    set_channel(name, channel)

def on_get_channel(data: tuple[str, dict[str, Any]]) -> None

源代码
@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == 'get_channel')
def on_get_channel(data: tuple[str, dict[str, Any]]):
    (name, recv_chan) = (data[1]['name'], data[1]['recv_chan'])
    recv_chan.send(get_channel(name))

def on_get_channels(data: tuple[str, dict[str, Any]]) -> None

源代码
@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == 'get_channels')
def on_get_channels(data: tuple[str, dict[str, Any]]):
    recv_chan = data[1]['recv_chan']
    recv_chan.send(get_channels())

def decorator(func: Callable[[T], Any]) -> Callable[[T], Any]

源代码
def decorator(func: Callable[[T], Any]) -> Callable[[T], Any]:
    global _func_id

    async def wrapper(data: T) -> Any:
        if filter_func is not None:
            if is_coroutine_callable(filter_func):
                if not await filter_func(data):
                    return
            elif not filter_func(data):
                return
        if is_coroutine_callable(func):
            return await func(data)
        else:
            return func(data)
    _callback_funcs[_func_id] = wrapper
    if IS_MAIN_PROCESS:
        self._on_main_receive_funcs.append(_func_id)
    else:
        self._on_sub_receive_funcs.append(_func_id)
    _func_id += 1
    return func

async def wrapper(data: T) -> Any

源代码
async def wrapper(data: T) -> Any:
    if filter_func is not None:
        if is_coroutine_callable(filter_func):
            if not await filter_func(data):
                return
        elif not filter_func(data):
            return
    if is_coroutine_callable(func):
        return await func(data)
    else:
        return func(data)

class Channel(Generic[T])

通道类,可以在进程间和进程内通信,双向但同时只能有一个发送者和一个接收者

有两种接收工作方式,但是只能选择一种,主动接收和被动接收,主动接收使用 receive 方法,被动接收使用 on_receive 装饰器

def __init__(self, _id: str, type_check: Optional[bool]) -> None

 初始化通道

Args:

_id: 通道ID

type_check: 是否开启类型检查, 若为空,则传入泛型默认开启,否则默认关闭
源代码
def __init__(self, _id: str, type_check: Optional[bool]=None):
    """
        初始化通道
        Args:
            _id: 通道ID
            type_check: 是否开启类型检查, 若为空,则传入泛型默认开启,否则默认关闭
        """
    (self.conn_send, self.conn_recv) = Pipe()
    self._closed = False
    self._on_main_receive_funcs: list[int] = []
    self._on_sub_receive_funcs: list[int] = []
    self.name: str = _id
    self.is_main_receive_loop_running = False
    self.is_sub_receive_loop_running = False
    if type_check is None:
        type_check = self._get_generic_type() is not None
    elif type_check:
        if self._get_generic_type() is None:
            raise TypeError('Type hint is required for enforcing type check.')
    self.type_check = type_check

def send(self, data: T) -> None

 发送数据

Args:

data: 数据
源代码
def send(self, data: T):
    """
        发送数据
        Args:
            data: 数据
        """
    if self.type_check:
        _type = self._get_generic_type()
        if _type is not None and (not self._validate_structure(data, _type)):
            raise TypeError(f'Data must be an instance of {_type}, {type(data)} found')
    if self._closed:
        raise RuntimeError('Cannot send to a closed channel_')
    self.conn_send.send(data)

def receive(self) -> T

 接收数据

Args:

源代码
def receive(self) -> T:
    """
        接收数据
        Args:
        """
    if self._closed:
        raise RuntimeError('Cannot receive from a closed channel_')
    while True:
        data = self.conn_recv.recv()
        return data

def close(self) -> None

 关闭通道

源代码
def close(self):
    """
        关闭通道
        """
    self._closed = True
    self.conn_send.close()
    self.conn_recv.close()

def on_receive(self, filter_func: Optional[FILTER_FUNC]) -> Callable[[Callable[[T], Any]], Callable[[T], Any]]

 接收数据并执行函数

Args:

filter_func: 过滤函数,为None则不过滤

Returns:

装饰器,装饰一个函数在接收到数据后执行
源代码
def on_receive(self, filter_func: Optional[FILTER_FUNC]=None) -> Callable[[Callable[[T], Any]], Callable[[T], Any]]:
    """
        接收数据并执行函数
        Args:
            filter_func: 过滤函数,为None则不过滤
        Returns:
            装饰器,装饰一个函数在接收到数据后执行
        """
    if not self.is_sub_receive_loop_running and (not IS_MAIN_PROCESS):
        threading.Thread(target=self._start_sub_receive_loop, daemon=True).start()
    if not self.is_main_receive_loop_running and IS_MAIN_PROCESS:
        threading.Thread(target=self._start_main_receive_loop, daemon=True).start()

    def decorator(func: Callable[[T], Any]) -> Callable[[T], Any]:
        global _func_id

        async def wrapper(data: T) -> Any:
            if filter_func is not None:
                if is_coroutine_callable(filter_func):
                    if not await filter_func(data):
                        return
                elif not filter_func(data):
                    return
            if is_coroutine_callable(func):
                return await func(data)
            else:
                return func(data)
        _callback_funcs[_func_id] = wrapper
        if IS_MAIN_PROCESS:
            self._on_main_receive_funcs.append(_func_id)
        else:
            self._on_sub_receive_funcs.append(_func_id)
        _func_id += 1
        return func
    return decorator

var T = TypeVar('T')

var channel_deliver_active_channel = Channel(_id='channel_deliver_active_channel')

var channel_deliver_passive_channel = Channel(_id='channel_deliver_passive_channel')

var recv_chan = data[1]['recv_chan']

var recv_chan = Channel[Channel[Any]]('recv_chan')

var recv_chan = Channel[dict[str, Channel[Any]]]('recv_chan')

var type_check = self._get_generic_type() is not None

var data = self.conn_recv.recv()

var func = _callback_funcs[func_id]

var func = _callback_funcs[func_id]

var data = self.conn_recv.recv()

var data = self.conn_recv.recv()

`,69),p=[c];function l(o,i){return a(),s("div",null,p)}const r=n(t,[["render",l],["__file","channel.html.vue"]]),d=JSON.parse(`{"path":"/dev/api/comm/channel.html","title":"liteyuki.comm.channel","lang":"zh-CN","frontmatter":{"title":"liteyuki.comm.channel","order":1,"icon":"laptop-code","category":"API","description":"def set_channel(name: str, channel: Channel) -> None 设置通道实例 Args: 源代码 def set_channels(channels: dict[str, Channel]) -> None 设置通道实例 Args: 源代码 def get_channel(name: str) -> Chann...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/comm/channel.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/comm/channel.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot 轻雪机器人"}],["meta",{"property":"og:title","content":"liteyuki.comm.channel"}],["meta",{"property":"og:description","content":"def set_channel(name: str, channel: Channel) -> None 设置通道实例 Args: 源代码 def set_channels(channels: dict[str, Channel]) -> None 设置通道实例 Args: 源代码 def get_channel(name: str) -> Chann..."}],["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-21T09:59:21.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-21T09:59:21.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.comm.channel\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-21T09:59:21.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"def set_channel(name: str, channel: Channel) -> None","slug":"def-set-channel-name-str-channel-channel-none","link":"#def-set-channel-name-str-channel-channel-none","children":[]},{"level":3,"title":"def set_channels(channels: dict[str, Channel]) -> None","slug":"def-set-channels-channels-dict-str-channel-none","link":"#def-set-channels-channels-dict-str-channel-none","children":[]},{"level":3,"title":"def get_channel(name: str) -> Channel","slug":"def-get-channel-name-str-channel","link":"#def-get-channel-name-str-channel","children":[]},{"level":3,"title":"def get_channels() -> dict[str, Channel]","slug":"def-get-channels-dict-str-channel","link":"#def-get-channels-dict-str-channel","children":[]},{"level":3,"title":"def on_set_channel(data: tuple[str, dict[str, Any]]) -> None","slug":"def-on-set-channel-data-tuple-str-dict-str-any-none","link":"#def-on-set-channel-data-tuple-str-dict-str-any-none","children":[]},{"level":3,"title":"def on_get_channel(data: tuple[str, dict[str, Any]]) -> None","slug":"def-on-get-channel-data-tuple-str-dict-str-any-none","link":"#def-on-get-channel-data-tuple-str-dict-str-any-none","children":[]},{"level":3,"title":"def on_get_channels(data: tuple[str, dict[str, Any]]) -> None","slug":"def-on-get-channels-data-tuple-str-dict-str-any-none","link":"#def-on-get-channels-data-tuple-str-dict-str-any-none","children":[]},{"level":3,"title":"def decorator(func: Callable[[T], Any]) -> Callable[[T], Any]","slug":"def-decorator-func-callable-t-any-callable-t-any","link":"#def-decorator-func-callable-t-any-callable-t-any","children":[]},{"level":3,"title":"async def wrapper(data: T) -> Any","slug":"async-def-wrapper-data-t-any","link":"#async-def-wrapper-data-t-any","children":[]},{"level":3,"title":"class Channel(Generic[T])","slug":"class-channel-generic-t","link":"#class-channel-generic-t","children":[]},{"level":3,"title":"def __init__(self, _id: str, type_check: Optional[bool]) -> None","slug":"def-init-self-id-str-type-check-optional-bool-none","link":"#def-init-self-id-str-type-check-optional-bool-none","children":[]},{"level":3,"title":"def send(self, data: T) -> None","slug":"def-send-self-data-t-none","link":"#def-send-self-data-t-none","children":[]},{"level":3,"title":"def receive(self) -> T","slug":"def-receive-self-t","link":"#def-receive-self-t","children":[]},{"level":3,"title":"def close(self) -> None","slug":"def-close-self-none","link":"#def-close-self-none","children":[]},{"level":3,"title":"def on_receive(self, filter_func: Optional[FILTER_FUNC]) -> Callable[[Callable[[T], Any]], Callable[[T], Any]]","slug":"def-on-receive-self-filter-func-optional-filter-func-callable-callable-t-any-callable-t-any","link":"#def-on-receive-self-filter-func-optional-filter-func-callable-callable-t-any-callable-t-any","children":[]},{"level":3,"title":"var T = TypeVar('T')","slug":"var-t-typevar-t","link":"#var-t-typevar-t","children":[]},{"level":3,"title":"var channel_deliver_active_channel = Channel(_id='channel_deliver_active_channel')","slug":"var-channel-deliver-active-channel-channel-id-channel-deliver-active-channel","link":"#var-channel-deliver-active-channel-channel-id-channel-deliver-active-channel","children":[]},{"level":3,"title":"var channel_deliver_passive_channel = Channel(_id='channel_deliver_passive_channel')","slug":"var-channel-deliver-passive-channel-channel-id-channel-deliver-passive-channel","link":"#var-channel-deliver-passive-channel-channel-id-channel-deliver-passive-channel","children":[]},{"level":3,"title":"var recv_chan = data[1]['recv_chan']","slug":"var-recv-chan-data-1-recv-chan","link":"#var-recv-chan-data-1-recv-chan","children":[]},{"level":3,"title":"var recv_chan = Channel[Channel[Any]]('recv_chan')","slug":"var-recv-chan-channel-channel-any-recv-chan","link":"#var-recv-chan-channel-channel-any-recv-chan","children":[]},{"level":3,"title":"var recv_chan = Channel[dict[str, Channel[Any]]]('recv_chan')","slug":"var-recv-chan-channel-dict-str-channel-any-recv-chan","link":"#var-recv-chan-channel-dict-str-channel-any-recv-chan","children":[]},{"level":3,"title":"var type_check = self._get_generic_type() is not None","slug":"var-type-check-self-get-generic-type-is-not-none","link":"#var-type-check-self-get-generic-type-is-not-none","children":[]},{"level":3,"title":"var data = self.conn_recv.recv()","slug":"var-data-self-conn-recv-recv","link":"#var-data-self-conn-recv-recv","children":[]},{"level":3,"title":"var func = _callback_funcs[func_id]","slug":"var-func-callback-funcs-func-id","link":"#var-func-callback-funcs-func-id","children":[]},{"level":3,"title":"var func = _callback_funcs[func_id]","slug":"var-func-callback-funcs-func-id-1","link":"#var-func-callback-funcs-func-id-1","children":[]},{"level":3,"title":"var data = self.conn_recv.recv()","slug":"var-data-self-conn-recv-recv-1","link":"#var-data-self-conn-recv-recv-1","children":[]},{"level":3,"title":"var data = self.conn_recv.recv()","slug":"var-data-self-conn-recv-recv-2","link":"#var-data-self-conn-recv-recv-2","children":[]}],"git":{"createdTime":1724031826000,"updatedTime":1724234361000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":6}]},"readingTime":{"minutes":3.83,"words":1150},"filePathRelative":"dev/api/comm/channel.md","localizedDate":"2024年8月19日","autoDesc":true}`);export{r as comp,d as data};