Skip to main content

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 async-message-handler
  • AMH creates asynchronous connection between asyncio event loop and another python process/thread.

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, Events

#Creating new command handler object.
handler = ProcessCommandHandler(timeout=15)

def sample_process(context:CommandContext):
    #You should initialize context to be bound.
    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.")

    time.sleep(5)
    #Send message from process to asynchronous context.
    context.send_message("This is a late message from process.")


#This is a sample event handler for new messages from process/thread.
@handler.on(Events.NewMessage)
def message_callback(message):
    print(message)


async def main():
    #This is asyncio context.
    response = await handler.command("Hello from asyncio.")
    print(response)
    #Waiting for a while for sample process to loop message demo.
    await asyncio.sleep(8)



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()

Metadata

Release files for async-message-handler 0.1.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 async-message-handler 0.1.1
File Size Uploaded
async-message-handler-0.1.1.tar.gz 4.3 kB Details

Release files / async-message-handler-0.1.1.tar.gz

Download URL async-message-handler-0.1.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
82f40f156650651d8595d56433ba49accb603247bc7176bf0619e2bb3496fcc8
BLAKE2b-256 checksum
How to use checksums
feef0b6c31dbceb6617bec81bc7c3a0f9c3f4e9354ee8d8dd79ac4e8b4fa7e82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

0.1.0

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