Skip to main content

介绍

本项目提供Python进程间通信的基础功能,适用于多进程编程中一主多从的编程模型。

项目中的Server可在主进程启动,Client在(多个)子进程中启动,客户端发送的消息能够被服务端接收, 并且对于不同的消息类型,可以注册不同的回调逻辑。 项目预置了一些常用通信协议,同时也提供简便的自定义通信协议的接口。

进程间通信基于unix socket实现,因此不能在windows系统中运行。

Quick Start

  1. 启动服务端
from dip import Server
import asyncio

def cb(req, mtype, data):
    print(data['hello'])

server = Server('/tmp/run.sock', callback=cb)
asyncio.run(server.serve_forever())
  1. 使用客户端发送消息
from dip import Client

cli = Client('/tmp/run.sock')
cli.send_json({'hello': 'world'})

服务端将会输出

>>> 'world'

自定义通信协议

如果需要自定义进程间通信时传输字节流的格式,可以使用自定义Protocol

import typing
from dip import Protocol

class NewProtocol(Protocol, mtype=b'N'):
    @classmethod
    def _decode(cls, buf: bytes) -> typing.Any:
        pass
    
    @classmethod
    def _encode(cls, data: typing.Any) -> bytes:
        pass

自定义通信协议需要使用一个byte作为标识(mtype),并且必须实现_decode和_encode两个方法。其中:

  1. _encode 用于将python对象序列化为字节流
  2. _decode 用于将字节流反序列化为python对象

两者一般互为逆操作,即对于python对象obj:

obj == Protocol.decode(Protocol.encode(obj))

注: 这并不是硬性规定,只是一种通常做法

下述是一个非常粗糙的固定字符串压缩协议的实现

from dip import Protocol
from dip import errors

class StringMappingProto(Protocol, mtype=b'S'):
    MAP = {
        'Hello World': b'h',
    }
    MAP_REV = {
        v: k for k, v in MAP.items()
    }
    
    @classmethod
    def _decode(cls, buf: bytes) -> str:
        data = cls.MAP_REV.get(buf)
        if data is not None:
            return data
        else:
            raise errors.ProtoDecodeError(f'Cannot decode byte: {buf!r}')

    @classmethod
    def _encode(cls, data: str) -> bytes:
        compressed = cls.MAP.get(data)
        if compressed is not None:
            return compressed
        else:
            raise errors.ProtoEncodeError(f'Cannot encode string: {data!r}')

使用客户端发送消息:

cli.send_msg('S', 'Hello World')

将会在服务端接收到消息 Hello World

Release files for deepfos-ipc-protocol 1.2.1

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

Source distribution (sdist)

Source distribution for deepfos-ipc-protocol 1.2.1
File Size Uploaded
deepfos-ipc-protocol-1.2.1.tar.gz 14.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for deepfos-ipc-protocol 1.2.1
File Interpreter ABI Platform
deepfos_ipc_protocol-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 24.2 kB

Release files / deepfos-ipc-protocol-1.2.1.tar.gz

Download URL deepfos-ipc-protocol-1.2.1.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
76e51f8a691b8685b38004dbe57ef8a3412ac7f773092ce6418c47714e322f4e
BLAKE2b-256 checksum
How to use checksums
2e502ee2c456db7ec3de6363ca9ee76105ab74a222ad83159c6e17c7c42a1d86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.2

Release files / deepfos_ipc_protocol-1.2.1-py3-none-any.whl

Download URL deepfos_ipc_protocol-1.2.1-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eff3eed897b291d3b4384dd4a865bc7ec3694e389445903b02b591f77007f6aa
BLAKE2b-256 checksum
How to use checksums
91e0cd190e88d87431136343c9e68446e7edd3f23f0d940fbbdb0c2947a3f707
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.2

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.0.13

2 release files

1.0.12

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

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