Skip to main content

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

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

async-message-handler-0.1.1.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file async-message-handler-0.1.1.tar.gz.

File metadata

  • Download URL: async-message-handler-0.1.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for async-message-handler-0.1.1.tar.gz
Algorithm Hash digest
SHA256 82f40f156650651d8595d56433ba49accb603247bc7176bf0619e2bb3496fcc8
MD5 d198f7cccfa3e37232b46f0197672c01
BLAKE2b-256 feef0b6c31dbceb6617bec81bc7c3a0f9c3f4e9354ee8d8dd79ac4e8b4fa7e82

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page