1

I'm new in python and I have to create a program that keeps in linstening from web socket and pipe so I need two asynchronous functions. Each of these functions call other method in different thrad that elaborate the content of received json. i.e. I receive a message on socket thread, I get the message and throw a new thread to elaborate the message.
This is the actual code:

import asyncio
import sys
import json
import websockets

# Keep listening from web socket and pipe


async def socket_receiver():
    """Listening from web socket"""
    file_socket = open(r"SocketReceived.txt", "w")
    header = {"Authorization": r"Basic XXXXXXXXXXXXXX="}
    async with websockets.connect(
            'wss://XXXXXXXXX', extra_headers=header) as web_socket:
        print("SOCKET receiving:")
        greeting = await web_socket.recv()
        json_message = json.loads(greeting)
        file_socket.write(json_message)
        print(json_message)

    file_socket.close()

async def pipe_receiver():
    """Listening from pipe"""
    file_pipe = open(r"ipeReceived.txt", "w")
    while True:
        print("PIPE receiving:")
        line = sys.stdin.readline()
        if not line:
            break

        jsonObj = json.loads(line);
        file_pipe.write(jsonObj['prova'] + '\n')
        # jsonValue = json.dump(str(line), file);
        sys.stdout.flush()

    file_pipe.close()
asyncio.get_event_loop().run_until_complete(socket_receiver())
asyncio.get_event_loop().run_until_complete(pipe_receiver())

run_until_complete method keep forever in my case (it waits the end of function), so only the socket starts. How can I start both? Thanks

luca
  • 3,248
  • 10
  • 66
  • 145
  • Possible duplicate of [Asyncio two loops for different I/O tasks?](https://stackoverflow.com/questions/31623194/asyncio-two-loops-for-different-i-o-tasks) - does that help? – kabanus Sep 06 '18 at 08:32
  • I prefer a more clear solution because I have this two listening method but each throw a new thread for every received message, I'm just watching even thread pool – luca Sep 06 '18 at 08:39

1 Answers1

2

asyncio.gather does the trick, the only point is that both functions should share the same event loop, and both should be fully asynchronous.

asyncio.get_event_loop().run_until_complete(
    asyncio.gather( socket_receiver(),pipe_receiver()))

From a quick reading of pipe_receiver, you will hang your event loop in sys.stdin.readline call, please consider using aioconsole to asynchronously handle the input.

  • the socket_receiver doesn't go over async with websockets.connect instruction, so only pipe works even if I removthe readline – luca Sep 07 '18 at 09:03
  • Use aiohttp web socket client ( https://aiohttp.readthedocs.io/en/v0.18.2/client_websockets.html ) instead the one of websockets packet and everything should work fine. – Jon Ander Ortiz Durántez Sep 07 '18 at 10:44