Asynchronous message handler between Process/Thread and asyncio event loop.
Project description
Asynchronous Message Handler
This package is a niche message handler solution between asyncio event loop and processes/threads (Aka multiprocessing.Process/threading.Thread).
You can install this package by
pip install asynchronous-message-handler
-
AMH creates asynchronous connection between asyncio event loop and another python process/thread.
-
For now, AMH only supports sending commands or primitive payloads from asyncio event loop to Process/Thread, and Process can respond incoming ProcessCommand object.
How to use?
Code below shows basic usage of message handler api. This message api is completely nonblocking in asyncio event loop.
import time
import asyncio
import multiprocessing as mp
from asynchronous_message_handler import ProcessCommandHandler, CommandContext, ProcessCommand
#Creating new command handler object.
handler = ProcessCommandHandler(timeout=15)
def sample_process(context:CommandContext):
#You should initialize context to listen for messages.
context.initialize()
#Waits until new message, this operation is blocking for this process.
#If you want it to be nonblocking use blocking=False parameter.
command:ProcessCommand = context.get_next()
print(command.get_payload())
#Sleep for 5 seconds.
time.sleep(5)
command.reply("Hello from process.")
async def main():
#This is asyncio context.
response = await handler.command("Hello from asyncio.")
print(response)
if __name__=="__main__":
process = mp.Process(target=sample_process, args=(handler.get_command_context(),))
process.start()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
process.join()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for async-message-handler-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2287d45a76c2fd9c25b6deb703035ee9e2cb91c510b2660521d9dbc8091a305 |
|
MD5 | 8e6866f9f095f539da6b63e202b490de |
|
BLAKE2b-256 | 57edc12181d89dba81d8970fdd9afba6309bf0bc8db9f5be87b79e1d0b2e8fb0 |