Skip to main content

Python 的基本 IPC 实现

项目描述

PyPI 版本信息 PyPI 支持的 Python 版本 文件状态

使用多处理和异步的优雅和现代的 Python IPC 实现。IPyC 有两种风格,同步和异步,都使用相同的后端,允许您根据需要进行选择。

发送内置函数、自定义对象等等!

主要特征

  • 使用现代asyncawait AsyncIO Python API。

  • 包括用于向后兼容的同步版本。

  • 灵活、易于安装、设置和使用。

  • 可以在运行时传输自定义对象和类!

安装

如果使用异步版本,则需要 Python 3.5.3 或更高版本

要安装库,您只需运行以下命令:

# In general
pip3 install IPyC

# Linux/macOS
python3 -m pip install -U IPyC

# Windows
py -3 -m pip install -U IPyC

Hello World 示例(使用异步版本)

客户

import asyncio
from ipyc import AsyncIPyCClient

async def hello_world():
    client = AsyncIPyCClient()  # Create a client
    link = await client.connect()  # Connect to the host
    await link.send("Hello World!")  # Send a string
    await client.close()  # Close the connection

loop = asyncio.get_event_loop()
loop.run_until_complete(hello_world())

主持人

import datetime
from ipyc import AsyncIPyCHost, AsyncIPyCLink

host = AsyncIPyCHost()

@host.on_connect
async def on_client_connect(connection: AsyncIPyCLink):
    while connection.is_active():
        message = await connection.receive()
        if message:
            print(f"[{datetime.datetime.now()}] - Client says: {message}")
    print(f"[{datetime.datetime.now()}] - Connection was closed!")

host.run()

自定义对象示例(使用同步版本)

自定义对象

class CustomObject:
    def __init__(self, arg1: int, arg2: float, arg3: str, arg4: set):
        self.a1 = arg1
        self.a2 = arg2
        self.a3 = arg3
        self.a4 = arg4

    def __str__(self):
        return f"<CustomObject:a1={self.a1},a2={self.a2},a3={self.a3},a4={self.a4}>"

    # Define a serializer that returns a string/str representation of the object
    def serialize(self):
        return f"{self.a1}|{self.a2}|{self.a3}|{self.a4}"

    # Define a deserializer that undoes what the serializer did and returns the object
    @staticmethod
    def deserialize(serialization):
        a1, a2, a3, a4 = serialization.split('|')
        return CustomObject(int(a1), float(a2), str(a3), eval(a4))

客户

from ipyc import IPyCClient, IPyCSerialization

custom_object = CustomObject(42, 3.1415926535897932, "Lorem ipsum dolor sit amet", {'s', 'e', 't'})
IPyCSerialization.add_custom_serialization(CustomObject, CustomObject.serialize)

client = IPyCClient()
link = client.connect()
link.send(custom_object)
client.close()

主持人

import datetime
from ipyc import IPyCHost, IPyCLink, IPyCSerialization

host = IPyCHost()
IPyCSerialization.add_custom_deserialization(CustomObject, CustomObject.deserialize)

while not host.is_closed():
    connection = host.wait_for_client()

    while connection.is_active():
        message = connection.receive()
        if message:
            print(f"[{datetime.datetime.now()}] - Client sent us a {type(message)} and it was {message}")
            print(message.a1, message.a2, message.a3, message.a4)
    print(f"[{datetime.datetime.now()}] - Connection was closed!")

您可以在示例目录中找到更多示例。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

IPyC-1.1.1.tar.gz (10.9 kB 查看哈希

已上传 source

内置分布

IPyC-1.1.1-py3-none-any.whl (13.4 kB 查看哈希

已上传 py3