Skip to main content

重置版本的Asyncio PSMB Client

同步的Guardian

from psmb_client.guardian import SyncGuardian
import asyncio

host = '127.0.0.1'
port = 13880
topic = '123123123'
client_id = 1

async def handler(msg: bytes):
    print(str(msg, encoding='UTF-8'))

guard = SyncGuardian(host, port, topic, client_id, handler)

guard.start()
guard.wait_available()
guard.send_msg("asdasdasd")
guard.join()

带自动重连的守护客户端

from psmb_client.guardian import Guardian
import asyncio

host = '127.0.0.1'
port = 13880
topic = '123123123'
client_id = 1

async def handler(msg: bytes):
    print(str(msg, encoding='UTF-8'))

guard = Guardian(host, port, topic, client_id, handler)

async def main():
    guard.try_connect()
    for _ in range(100):
        await guard.send_msg(b"hahaha")
        await asyncio.sleep(10)

asyncio.run(main())

使用高级抽象的Client

from psmb_client.stream import Client
import asyncio

host = '127.0.0.1'
port = 13880
topic = '123123123'
client_id = 1

async def handler(msg: bytes):
    print(str(msg, encoding='UTF-8'))

client = Client(host, port, topic, client_id, handler)

async def main():
    try:
        await client.establish()
    except IOError:
        return
    for _ in range(10):
        try:
            await client.send_msg(b'asdasd')
            await asyncio.sleep(5)
        except IOError:
            break
    try:
        await client.close()
    except BrokenPipeError:
        return

asyncio.run(main())

流式、分离的Client

from psmb_client.stream import *
import asyncio

host = '127.0.0.1'
port = 13880
topic = '123123123'
subsciber_id = 1

async def start_pub() -> Publisher:
    pub = Publisher(host, port, topic)
    await pub.open_connection()
    return pub

async def start_sub() -> Subscriber:

    async def process(data: bytes):
        print(str(data, 'UTF-8'))      
    sub = Subscriber(host, port, topic, subsciber_id, process)
    await sub.open_connection()
    return sub


async def main():
    pub, sub = await asyncio.gather(start_pub(), start_sub())
    for i in range(10):
        try:
            await pub.send_msg(b'asdasd')
            await asyncio.sleep(5)
        except IOError:
            break
    try:
        await pub.close()
        await sub.close()
    except BrokenPipeError:
        pass

asyncio.run(main())

Release files for psmb-client 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for psmb-client 0.3.2
File Interpreter ABI Platform
psmb_client-0.3.2-py3-none-any.whl Python 3 none any Details

Release files / psmb_client-0.3.2-py3-none-any.whl

Download URL psmb_client-0.3.2-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
41bfc0b03ed4c0d8e0af230197ed3217e65df40cb0a0bb3ee34bfaed1f8717f5
BLAKE2b-256 checksum
How to use checksums
4d812eb3b9873f263232169e1f7033e935ba422b1a64fa321bb4cd7061269c10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.3.2 This release

1 release file

0.3.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page