A basic IPC implementation for Python
Project description
An elegant and modern Python IPC implementation using multiprocessing and asyncio. IPyC comes in two flavors, synchronous and asynchronous, both using the same backend allowing you to pick and chose to your needs.
Send builtins, custom objects, and more!
Key Features
Uses the modern async and await AsyncIO Python API.
Includes a synchronous version for backward compatibility.
Flexible, easy to install, setup, and use.
Can transfer custom objects and classes at runtime!
Installing
Python 3.5.3 or higher is required if you use the asynchronous version
To install the library you can just run the following command:
# In general
pip3 install IPyC
# Linux/macOS
python3 -m pip install -U IPyC
# Windows
py -3 -m pip install -U IPyC
Hello World Example (using the asynchronous version)
Client
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())
Host
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()
Custom Objects Example (using the synchronous version)
Custom Object
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))
Client
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()
Host
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!")
You can find more examples in the examples directory.
Useful Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file IPyC-1.1.1.tar.gz
.
File metadata
- Download URL: IPyC-1.1.1.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.22.0 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fbf27dc0f0ca5f79a32a25b969bca93239d1de2761420c0f907e896ae79561a |
|
MD5 | 564cbab3fed0bef3bd17538dfaa9d658 |
|
BLAKE2b-256 | 8581cffc23d5487d2adcf519c5878a194d44784b9e637e1bf6616a7865195617 |
File details
Details for the file IPyC-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: IPyC-1.1.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.22.0 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2fc2475fe9d17a292f5f15f643e37d605789867a9193d2e96e687fad14a29b2f |
|
MD5 | 633fa87baca7d62583d44568ed7a8b73 |
|
BLAKE2b-256 | 6f60d5278d928c5211412981f4e3c1652ed2b9cfe3c7b00af2cae3098c8eb3f5 |