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

def run_subscriber_receive_funcs(channel_: str, data: Any) -> None

运行订阅者接收函数

Args:

channel_: 频道

data: 数据
源代码
@staticmethod
def run_subscriber_receive_funcs(channel_: str, data: Any):
    """
        运行订阅者接收函数
        Args:
            channel_: 频道
            data: 数据
        """
    if IS_MAIN_PROCESS:
        if channel_ in _on_main_subscriber_receive_funcs and _on_main_subscriber_receive_funcs[channel_]:
            run_coroutine_in_thread(*[func(data) for func in _on_main_subscriber_receive_funcs[channel_]])
    elif channel_ in _on_sub_subscriber_receive_funcs and _on_sub_subscriber_receive_funcs[channel_]:
        run_coroutine_in_thread(*[func(data) for func in _on_sub_subscriber_receive_funcs[channel_]])

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

源代码
@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'get')
def on_get(data: tuple[str, dict[str, Any]]):
    key = data[1]['key']
    default = data[1]['default']
    recv_chan = data[1]['recv_chan']
    recv_chan.send(shared_memory.get(key, default))

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

源代码
@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'set')
def on_set(data: tuple[str, dict[str, Any]]):
    key = data[1]['key']
    value = data[1]['value']
    shared_memory.set(key, value)

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

源代码
@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'delete')
def on_delete(data: tuple[str, dict[str, Any]]):
    key = data[1]['key']
    shared_memory.delete(key)

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

源代码
@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'get_all')
def on_get_all(data: tuple[str, dict[str, Any]]):
    recv_chan = data[1]['recv_chan']
    recv_chan.send(shared_memory.get_all())

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

源代码
@channel.publish_channel.on_receive()
def on_publish(data: tuple[str, Any]):
    (channel_, data) = data
    shared_memory.run_subscriber_receive_funcs(channel_, data)

def decorator(func: ON_RECEIVE_FUNC) -> ON_RECEIVE_FUNC

源代码
def decorator(func: ON_RECEIVE_FUNC) -> ON_RECEIVE_FUNC:

    async def wrapper(data: Any):
        if is_coroutine_callable(func):
            await func(data)
        else:
            func(data)
    if IS_MAIN_PROCESS:
        if channel_ not in _on_main_subscriber_receive_funcs:
            _on_main_subscriber_receive_funcs[channel_] = []
        _on_main_subscriber_receive_funcs[channel_].append(wrapper)
    else:
        if channel_ not in _on_sub_subscriber_receive_funcs:
            _on_sub_subscriber_receive_funcs[channel_] = []
        _on_sub_subscriber_receive_funcs[channel_].append(wrapper)
    return wrapper

async def wrapper(data: Any) -> None

源代码
async def wrapper(data: Any):
    if is_coroutine_callable(func):
        await func(data)
    else:
        func(data)

class Subscriber

def __init__(self) -> None

源代码
def __init__(self):
    self._subscribers = {}

def receive(self) -> Any

源代码
def receive(self) -> Any:
    pass

def unsubscribe(self) -> None

源代码
def unsubscribe(self) -> None:
    pass

class KeyValueStore

def __init__(self) -> None

源代码
def __init__(self):
    self._store = {}
    self.active_chan = Channel[tuple[str, Optional[dict[str, Any]]]](_id='shared_memory-active')
    self.passive_chan = Channel[tuple[str, Optional[dict[str, Any]]]](_id='shared_memory-passive')
    self.publish_channel = Channel[tuple[str, Any]](_id='shared_memory-publish')
    self.is_main_receive_loop_running = False
    self.is_sub_receive_loop_running = False

def set(self, key: str, value: Any) -> None

 设置键值对

Args:

key: 键

value: 值
源代码
def set(self, key: str, value: Any) -> None:
    """
        设置键值对
        Args:
            key: 键
            value: 值

        """
    if IS_MAIN_PROCESS:
        lock = _get_lock(key)
        with lock:
            self._store[key] = value
    else:
        self.passive_chan.send(('set', {'key': key, 'value': value}))

def get(self, key: str, default: Optional[Any]) -> Optional[Any]

 获取键值对

Args:

key: 键

default: 默认值

Returns:

Any: 值
源代码
def get(self, key: str, default: Optional[Any]=None) -> Optional[Any]:
    """
        获取键值对
        Args:
            key: 键
            default: 默认值

        Returns:
            Any: 值
        """
    if IS_MAIN_PROCESS:
        lock = _get_lock(key)
        with lock:
            return self._store.get(key, default)
    else:
        recv_chan = Channel[Optional[Any]]('recv_chan')
        self.passive_chan.send(('get', {'key': key, 'default': default, 'recv_chan': recv_chan}))
        return recv_chan.receive()

def delete(self, key: str, ignore_key_error: bool) -> None

 删除键值对

Args:

key: 键

ignore_key_error: 是否忽略键不存在的错误

Returns:

源代码
def delete(self, key: str, ignore_key_error: bool=True) -> None:
    """
        删除键值对
        Args:
            key: 键
            ignore_key_error: 是否忽略键不存在的错误

        Returns:
        """
    if IS_MAIN_PROCESS:
        lock = _get_lock(key)
        with lock:
            if key in self._store:
                try:
                    del self._store[key]
                    del _locks[key]
                except KeyError as e:
                    if not ignore_key_error:
                        raise e
    else:
        self.passive_chan.send(('delete', {'key': key}))

def get_all(self) -> dict[str, Any]

 获取所有键值对

Returns:

dict[str, Any]: 键值对
源代码
def get_all(self) -> dict[str, Any]:
    """
        获取所有键值对
        Returns:
            dict[str, Any]: 键值对
        """
    if IS_MAIN_PROCESS:
        return self._store
    else:
        recv_chan = Channel[dict[str, Any]]('recv_chan')
        self.passive_chan.send(('get_all', {'recv_chan': recv_chan}))
        return recv_chan.receive()

def publish(self, channel_: str, data: Any) -> None

 发布消息

Args:

channel_: 频道

data: 数据

Returns:

源代码
def publish(self, channel_: str, data: Any) -> None:
    """
        发布消息
        Args:
            channel_: 频道
            data: 数据

        Returns:
        """
    self.active_chan.send(('publish', {'channel': channel_, 'data': data}))

def on_subscriber_receive(self, channel_: str) -> Callable[[ON_RECEIVE_FUNC], ON_RECEIVE_FUNC]

 订阅者接收消息时的回调

Args:

channel_: 频道

Returns:

装饰器
源代码
def on_subscriber_receive(self, channel_: str) -> Callable[[ON_RECEIVE_FUNC], ON_RECEIVE_FUNC]:
    """
        订阅者接收消息时的回调
        Args:
            channel_: 频道

        Returns:
            装饰器
        """
    if IS_MAIN_PROCESS and (not self.is_main_receive_loop_running):
        threading.Thread(target=self._start_receive_loop, daemon=True).start()
        shared_memory.is_main_receive_loop_running = True
    elif not IS_MAIN_PROCESS and (not self.is_sub_receive_loop_running):
        threading.Thread(target=self._start_receive_loop, daemon=True).start()
        shared_memory.is_sub_receive_loop_running = True

    def decorator(func: ON_RECEIVE_FUNC) -> ON_RECEIVE_FUNC:

        async def wrapper(data: Any):
            if is_coroutine_callable(func):
                await func(data)
            else:
                func(data)
        if IS_MAIN_PROCESS:
            if channel_ not in _on_main_subscriber_receive_funcs:
                _on_main_subscriber_receive_funcs[channel_] = []
            _on_main_subscriber_receive_funcs[channel_].append(wrapper)
        else:
            if channel_ not in _on_sub_subscriber_receive_funcs:
                _on_sub_subscriber_receive_funcs[channel_] = []
            _on_sub_subscriber_receive_funcs[channel_].append(wrapper)
        return wrapper
    return decorator

@staticmethod

def run_subscriber_receive_funcs(channel_: str, data: Any) -> None

 运行订阅者接收函数

Args:

channel_: 频道

data: 数据
源代码
@staticmethod
def run_subscriber_receive_funcs(channel_: str, data: Any):
    """
        运行订阅者接收函数
        Args:
            channel_: 频道
            data: 数据
        """
    if IS_MAIN_PROCESS:
        if channel_ in _on_main_subscriber_receive_funcs and _on_main_subscriber_receive_funcs[channel_]:
            run_coroutine_in_thread(*[func(data) for func in _on_main_subscriber_receive_funcs[channel_]])
    elif channel_ in _on_sub_subscriber_receive_funcs and _on_sub_subscriber_receive_funcs[channel_]:
        run_coroutine_in_thread(*[func(data) for func in _on_sub_subscriber_receive_funcs[channel_]])

class GlobalKeyValueStore

@classmethod

def get_instance(cls: Any) -> None

源代码
@classmethod
def get_instance(cls):
    if cls._instance is None:
        with cls._lock:
            if cls._instance is None:
                cls._instance = KeyValueStore()
    return cls._instance

attr _instance: None

attr _lock: threading.Lock()

var key = data[1]['key']

var default = data[1]['default']

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

var key = data[1]['key']

var value = data[1]['value']

var key = data[1]['key']

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

var lock = _get_lock(key)

var lock = _get_lock(key)

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

var lock = _get_lock(key)

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

var data = self.active_chan.receive()

var data = self.publish_channel.receive()

`,96),c=[p];function o(l,i){return s(),a("div",null,c)}const r=n(t,[["render",o],["__file","storage.html.vue"]]),d=JSON.parse(`{"path":"/en/dev/api/comm/storage.html","title":"liteyuki.comm.storage","lang":"en-US","frontmatter":{"title":"liteyuki.comm.storage","order":1,"icon":"laptop-code","category":"API","description":"def run_subscriber_receive_funcs(channel_: str, data: Any) -> None 运行订阅者接收函数 Args: 源代码 def on_get(data: tuple[str, dict[str, Any]]) -> None 源代码 def on_set(data: tuple[str, dict[...","head":[["link",{"rel":"alternate","hreflang":"zh-cn","href":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/comm/storage.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/comm/storage.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot"}],["meta",{"property":"og:title","content":"liteyuki.comm.storage"}],["meta",{"property":"og:description","content":"def run_subscriber_receive_funcs(channel_: str, data: Any) -> None 运行订阅者接收函数 Args: 源代码 def on_get(data: tuple[str, dict[str, Any]]) -> None 源代码 def on_set(data: tuple[str, dict[..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"en-US"}],["meta",{"property":"og:locale:alternate","content":"zh-CN"}],["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.storage\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-21T09:59:21.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"def run_subscriber_receive_funcs(channel_: str, data: Any) -> None","slug":"def-run-subscriber-receive-funcs-channel-str-data-any-none","link":"#def-run-subscriber-receive-funcs-channel-str-data-any-none","children":[]},{"level":3,"title":"def on_get(data: tuple[str, dict[str, Any]]) -> None","slug":"def-on-get-data-tuple-str-dict-str-any-none","link":"#def-on-get-data-tuple-str-dict-str-any-none","children":[]},{"level":3,"title":"def on_set(data: tuple[str, dict[str, Any]]) -> None","slug":"def-on-set-data-tuple-str-dict-str-any-none","link":"#def-on-set-data-tuple-str-dict-str-any-none","children":[]},{"level":3,"title":"def on_delete(data: tuple[str, dict[str, Any]]) -> None","slug":"def-on-delete-data-tuple-str-dict-str-any-none","link":"#def-on-delete-data-tuple-str-dict-str-any-none","children":[]},{"level":3,"title":"def on_get_all(data: tuple[str, dict[str, Any]]) -> None","slug":"def-on-get-all-data-tuple-str-dict-str-any-none","link":"#def-on-get-all-data-tuple-str-dict-str-any-none","children":[]},{"level":3,"title":"def on_publish(data: tuple[str, Any]) -> None","slug":"def-on-publish-data-tuple-str-any-none","link":"#def-on-publish-data-tuple-str-any-none","children":[]},{"level":3,"title":"def decorator(func: ON_RECEIVE_FUNC) -> ON_RECEIVE_FUNC","slug":"def-decorator-func-on-receive-func-on-receive-func","link":"#def-decorator-func-on-receive-func-on-receive-func","children":[]},{"level":3,"title":"async def wrapper(data: Any) -> None","slug":"async-def-wrapper-data-any-none","link":"#async-def-wrapper-data-any-none","children":[]},{"level":3,"title":"class Subscriber","slug":"class-subscriber","link":"#class-subscriber","children":[]},{"level":3,"title":"def __init__(self) -> None","slug":"def-init-self-none","link":"#def-init-self-none","children":[]},{"level":3,"title":"def receive(self) -> Any","slug":"def-receive-self-any","link":"#def-receive-self-any","children":[]},{"level":3,"title":"def unsubscribe(self) -> None","slug":"def-unsubscribe-self-none","link":"#def-unsubscribe-self-none","children":[]},{"level":3,"title":"class KeyValueStore","slug":"class-keyvaluestore","link":"#class-keyvaluestore","children":[]},{"level":3,"title":"def __init__(self) -> None","slug":"def-init-self-none-1","link":"#def-init-self-none-1","children":[]},{"level":3,"title":"def set(self, key: str, value: Any) -> None","slug":"def-set-self-key-str-value-any-none","link":"#def-set-self-key-str-value-any-none","children":[]},{"level":3,"title":"def get(self, key: str, default: Optional[Any]) -> Optional[Any]","slug":"def-get-self-key-str-default-optional-any-optional-any","link":"#def-get-self-key-str-default-optional-any-optional-any","children":[]},{"level":3,"title":"def delete(self, key: str, ignore_key_error: bool) -> None","slug":"def-delete-self-key-str-ignore-key-error-bool-none","link":"#def-delete-self-key-str-ignore-key-error-bool-none","children":[]},{"level":3,"title":"def get_all(self) -> dict[str, Any]","slug":"def-get-all-self-dict-str-any","link":"#def-get-all-self-dict-str-any","children":[]},{"level":3,"title":"def publish(self, channel_: str, data: Any) -> None","slug":"def-publish-self-channel-str-data-any-none","link":"#def-publish-self-channel-str-data-any-none","children":[]},{"level":3,"title":"def on_subscriber_receive(self, channel_: str) -> Callable[[ON_RECEIVE_FUNC], ON_RECEIVE_FUNC]","slug":"def-on-subscriber-receive-self-channel-str-callable-on-receive-func-on-receive-func","link":"#def-on-subscriber-receive-self-channel-str-callable-on-receive-func-on-receive-func","children":[]},{"level":3,"title":"@staticmethod","slug":"staticmethod","link":"#staticmethod","children":[]},{"level":3,"title":"def run_subscriber_receive_funcs(channel_: str, data: Any) -> None","slug":"def-run-subscriber-receive-funcs-channel-str-data-any-none-1","link":"#def-run-subscriber-receive-funcs-channel-str-data-any-none-1","children":[]},{"level":3,"title":"class GlobalKeyValueStore","slug":"class-globalkeyvaluestore","link":"#class-globalkeyvaluestore","children":[]},{"level":3,"title":"@classmethod","slug":"classmethod","link":"#classmethod","children":[]},{"level":3,"title":"def get_instance(cls: Any) -> None","slug":"def-get-instance-cls-any-none","link":"#def-get-instance-cls-any-none","children":[]},{"level":3,"title":"attr _instance: None","slug":"attr-instance-none","link":"#attr-instance-none","children":[]},{"level":3,"title":"attr _lock: threading.Lock()","slug":"attr-lock-threading-lock","link":"#attr-lock-threading-lock","children":[]},{"level":3,"title":"var key = data[1]['key']","slug":"var-key-data-1-key","link":"#var-key-data-1-key","children":[]},{"level":3,"title":"var default = data[1]['default']","slug":"var-default-data-1-default","link":"#var-default-data-1-default","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 key = data[1]['key']","slug":"var-key-data-1-key-1","link":"#var-key-data-1-key-1","children":[]},{"level":3,"title":"var value = data[1]['value']","slug":"var-value-data-1-value","link":"#var-value-data-1-value","children":[]},{"level":3,"title":"var key = data[1]['key']","slug":"var-key-data-1-key-2","link":"#var-key-data-1-key-2","children":[]},{"level":3,"title":"var recv_chan = data[1]['recv_chan']","slug":"var-recv-chan-data-1-recv-chan-1","link":"#var-recv-chan-data-1-recv-chan-1","children":[]},{"level":3,"title":"var lock = _get_lock(key)","slug":"var-lock-get-lock-key","link":"#var-lock-get-lock-key","children":[]},{"level":3,"title":"var lock = _get_lock(key)","slug":"var-lock-get-lock-key-1","link":"#var-lock-get-lock-key-1","children":[]},{"level":3,"title":"var recv_chan = Channel[Optional[Any]]('recv_chan')","slug":"var-recv-chan-channel-optional-any-recv-chan","link":"#var-recv-chan-channel-optional-any-recv-chan","children":[]},{"level":3,"title":"var lock = _get_lock(key)","slug":"var-lock-get-lock-key-2","link":"#var-lock-get-lock-key-2","children":[]},{"level":3,"title":"var recv_chan = Channel[dict[str, Any]]('recv_chan')","slug":"var-recv-chan-channel-dict-str-any-recv-chan","link":"#var-recv-chan-channel-dict-str-any-recv-chan","children":[]},{"level":3,"title":"var data = self.active_chan.receive()","slug":"var-data-self-active-chan-receive","link":"#var-data-self-active-chan-receive","children":[]},{"level":3,"title":"var data = self.publish_channel.receive()","slug":"var-data-self-publish-channel-receive","link":"#var-data-self-publish-channel-receive","children":[]}],"git":{"createdTime":1724234361000,"updatedTime":1724234361000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":1}]},"readingTime":{"minutes":4,"words":1200},"filePathRelative":"en/dev/api/comm/storage.md","localizedDate":"August 21, 2024","autoDesc":true}`);export{r as comp,d as data};