aio_pubsub
########
1. 安装
==========
.. code-block:: shell
pip install aiopubsub-py3
pip install aiopubsub-py3[redis]
2. 示例
==========
- 2.1 发布
.. code-block:: python
from aiopubsub import Pubsub
async def main():
async with Pubsub(Pubsub.REDIS, port=16379, namespace="cs", role=PubsubRole.PUB) as pub:
count = await pub.publish("foo", {"test": 1})
print(count)
count = await pub.publish("foo", {"test": 2})
print(count)
count = await pub.publish("foo", {"test": 3})
print(count)
- 2.2 订阅
.. code-block:: python
from aiopubsub import Pubsub, PubsubRole
async def print_msg(channel, msg):
print(f"sub msg: {channel} -- {msg}")
async def main():
channels = ["foo"]
async with Pubsub(Pubsub.REDIS, port=16379, namespace="cs", role=PubsubRole.SUB) as sub:
await sub.subscribe(*channels)
async for k in sub.listen(handler=print_msg):
print(k)
await sub.unsubscribe(*channels)
- 2.3 模糊订阅
.. code-block:: python
from aiopubsub import Pubsub
async def print_msg(channel, msg):
print(f"psub msg: {channel} -- {msg}")
async def main():
channels = ["foo*"]
async with Pubsub(Pubsub.REDIS, port=16379, namespace="cs", role=PubsubRole.SUB) as psub:
await psub.psubscribe(*channels)
async for k in psub.listen(handler=print_msg):
print(k)
await psub.unsubscribe(*channels)
- 2.4 自动识别角色【根据第一次调用函数决策,未close之前不能切换角色】
.. code-block:: python
from aiopubsub import Pubsub
async def print_msg(channel, msg):
print(f"psub msg: {channel} -- {msg}")
async def main():
channels = ["foo*"]
pubsub = Pubsub(Pubsub.REDIS, port=16379, namespace="cs")
await pubsub.publish("foo", {"test": 1}) # 角色设置为PUB,执行成功
await pubsub.subscribe(*channels) # 角色为PUB,无法订阅抛出异常
await pubsub.close() # 角色释放
await pubsub.subscribe(*channels) # 角色设置为SUB,执行成功
print(pubsub.backend.role)
await pubsub.publish("foo", {"test": 1}) # 角色为SUB,无法发布抛出异常
async with pubsub:
await pubsub.unsubscribe(*channels) # 角色为SUB,执行成功
# async with 退出作用域,角色释放
await pubsub.publish("foo", {"test": 1}) # 角色设置为PUB,执行成功
Raw data
{
"_id": null,
"home_page": "https://github.com/daleeg/aio_pubsub",
"name": "aiopubsub-py3",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "aio redis pubsub mqtt publish subscribe",
"author": "",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/88/af/d06decbfe6c5b9faa79addffabe2eb618ed9f05dcd036cfa6aef015ab1ec/aiopubsub-py3-1.0.9.tar.gz",
"platform": "POSIX",
"description": "aio_pubsub\n########\n\n\n1. \u5b89\u88c5\n==========\n\n.. code-block:: shell\n\n pip install aiopubsub-py3\n pip install aiopubsub-py3[redis]\n\n2. \u793a\u4f8b\n==========\n\n- 2.1 \u53d1\u5e03\n\n.. code-block:: python\n\n from aiopubsub import Pubsub\n\n async def main():\n async with Pubsub(Pubsub.REDIS, port=16379, namespace=\"cs\", role=PubsubRole.PUB) as pub:\n count = await pub.publish(\"foo\", {\"test\": 1})\n print(count)\n count = await pub.publish(\"foo\", {\"test\": 2})\n print(count)\n count = await pub.publish(\"foo\", {\"test\": 3})\n print(count)\n\n- 2.2 \u8ba2\u9605\n\n.. code-block:: python\n\n from aiopubsub import Pubsub, PubsubRole\n\n\n async def print_msg(channel, msg):\n print(f\"sub msg: {channel} -- {msg}\")\n\n async def main():\n channels = [\"foo\"]\n async with Pubsub(Pubsub.REDIS, port=16379, namespace=\"cs\", role=PubsubRole.SUB) as sub:\n await sub.subscribe(*channels)\n async for k in sub.listen(handler=print_msg):\n print(k)\n await sub.unsubscribe(*channels)\n\n- 2.3 \u6a21\u7cca\u8ba2\u9605\n\n.. code-block:: python\n\n from aiopubsub import Pubsub\n\n async def print_msg(channel, msg):\n print(f\"psub msg: {channel} -- {msg}\")\n\n async def main():\n channels = [\"foo*\"]\n async with Pubsub(Pubsub.REDIS, port=16379, namespace=\"cs\", role=PubsubRole.SUB) as psub:\n await psub.psubscribe(*channels)\n async for k in psub.listen(handler=print_msg):\n print(k)\n await psub.unsubscribe(*channels)\n\n\n- 2.4 \u81ea\u52a8\u8bc6\u522b\u89d2\u8272\u3010\u6839\u636e\u7b2c\u4e00\u6b21\u8c03\u7528\u51fd\u6570\u51b3\u7b56\uff0c\u672aclose\u4e4b\u524d\u4e0d\u80fd\u5207\u6362\u89d2\u8272\u3011\n\n.. code-block:: python\n\n from aiopubsub import Pubsub\n\n async def print_msg(channel, msg):\n print(f\"psub msg: {channel} -- {msg}\")\n\n async def main():\n channels = [\"foo*\"]\n pubsub = Pubsub(Pubsub.REDIS, port=16379, namespace=\"cs\")\n await pubsub.publish(\"foo\", {\"test\": 1}) # \u89d2\u8272\u8bbe\u7f6e\u4e3aPUB\uff0c\u6267\u884c\u6210\u529f\n\n await pubsub.subscribe(*channels) # \u89d2\u8272\u4e3aPUB\uff0c\u65e0\u6cd5\u8ba2\u9605\u629b\u51fa\u5f02\u5e38\n\n await pubsub.close() # \u89d2\u8272\u91ca\u653e\n\n await pubsub.subscribe(*channels) # \u89d2\u8272\u8bbe\u7f6e\u4e3aSUB\uff0c\u6267\u884c\u6210\u529f\n\n print(pubsub.backend.role)\n\n await pubsub.publish(\"foo\", {\"test\": 1}) # \u89d2\u8272\u4e3aSUB\uff0c\u65e0\u6cd5\u53d1\u5e03\u629b\u51fa\u5f02\u5e38\n\n async with pubsub:\n await pubsub.unsubscribe(*channels) # \u89d2\u8272\u4e3aSUB\uff0c\u6267\u884c\u6210\u529f\n # async with \u9000\u51fa\u4f5c\u7528\u57df\uff0c\u89d2\u8272\u91ca\u653e\n\n await pubsub.publish(\"foo\", {\"test\": 1}) # \u89d2\u8272\u8bbe\u7f6e\u4e3aPUB\uff0c\u6267\u884c\u6210\u529f\n\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "aio pubsub",
"version": "1.0.9",
"project_urls": {
"Homepage": "https://github.com/daleeg/aio_pubsub"
},
"split_keywords": [
"aio",
"redis",
"pubsub",
"mqtt",
"publish",
"subscribe"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "157db2729bad60fe3decf7882a84a4379f01c53d4e78eaf8fa3a95b60e17dc91",
"md5": "3a581a3480fc9762caf84047a2030ee8",
"sha256": "fd7656ffac1454d5622ecac288970672b34a6388ae58ca812204b080f993494d"
},
"downloads": -1,
"filename": "aiopubsub_py3-1.0.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3a581a3480fc9762caf84047a2030ee8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 8843,
"upload_time": "2023-11-13T03:48:15",
"upload_time_iso_8601": "2023-11-13T03:48:15.481817Z",
"url": "https://files.pythonhosted.org/packages/15/7d/b2729bad60fe3decf7882a84a4379f01c53d4e78eaf8fa3a95b60e17dc91/aiopubsub_py3-1.0.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "88afd06decbfe6c5b9faa79addffabe2eb618ed9f05dcd036cfa6aef015ab1ec",
"md5": "75bc74f2a2b3ed025b3555380392875d",
"sha256": "e533553632367893ce352beeb7250d25902790e35e533b15581607b371855d8c"
},
"downloads": -1,
"filename": "aiopubsub-py3-1.0.9.tar.gz",
"has_sig": false,
"md5_digest": "75bc74f2a2b3ed025b3555380392875d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 7977,
"upload_time": "2023-11-13T03:48:18",
"upload_time_iso_8601": "2023-11-13T03:48:18.577265Z",
"url": "https://files.pythonhosted.org/packages/88/af/d06decbfe6c5b9faa79addffabe2eb618ed9f05dcd036cfa6aef015ab1ec/aiopubsub-py3-1.0.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-13 03:48:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "daleeg",
"github_project": "aio_pubsub",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "aiopubsub-py3"
}