[Python] How to use socketio and ipc together in python

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to building an server that communicates with the client using socketio protocol, Now the server starts multiple application as a sub processes, the server communicates with this applications using ipc protocol. Now I call the ipc applications inside socketio handler, I want to wait for the ipc application to finish its computation and return me the data then I will process that data and send it back the to client using socketio. I have create a simple script to mimic what I am doing here.

    from mvent import MemoryEventHandler
    import random
    from blinker import signal, NamedSignal
    import threading
    import time

    def generate_4digit_number():
    """
    Generates a random 4-digit number between 1000 and 9999.
    """
    return random.randint(1000, 9999)

    service_event = MemoryEventHandler("testService")
    sig = signal("sig")

    class Communicator:
    def __init__(self, meh: MemoryEventHandler, sig: NamedSignal):
    self.meh = meh
    self.sig = sig

    def send(self, command, data):
    def signal_handler():
    self.meh.emit(command, data)
    result = None
    event_received = threading.Event()

    def on_prompt(data):
    nonlocal result
    result = data
    event_received.set()

    self.sig.connect(on_prompt, weak=False)
    event_received.wait()
    return result

    return signal_handler

    # service_event.emit("command:generate_prompt", {"id": generate_4digit_number()})

    com = Communicator(service_event, sig)

    handler = com.send("command:generate_prompt", {"id": generate_4digit_number()})
    result = handler()
    print(result)

    @service_event.on("response:generate_prompt")
    def on_prompt(data):
    sig.send(data)

    while True:
    pass



    Here I am getting None, it is not waiting for my ipc application to return the data.

    I am using a library that I am supposed to use in my company which is this https://pypi.org/project/mvent/, At this point I can drop this library for something better as well.

    What I am trying to do is create a class that I can call inside the socketio handler wait for the data then continue with the response.

    Continue reading...

Compartilhe esta Página