Home > database >  Python socket.io messages getting blocked after dumps
Python socket.io messages getting blocked after dumps

Time:02-26

I am trying to have two-way communication between two clients but as soon as the dumps I am no longer able to send any messages from the client file The send message is what I believe is blocking the get part. Anyone know how to fix this? Thanks in advance

(i am able to send message one time from client to client_led but not after that)

 @sio.event
    async def get_message(message):
        if clientName == message['from']:
            pass
        else:
            if "Client1" == message['from']:
                usb_serial.print_on_display(message['message'])
            #print(message['from'])

    async def send_message():
        while True:
            await asyncio.sleep(5)
            messageToSend = dumps(GPIO_read.get_control_code())
            await sio.emit('send_chat_room', {'message': messageToSend, 'name': clientName, 'room': roomName})

Full Scripts

CLIENT.PY


from socketio import AsyncClient
import asyncio
from json import dumps
from aioconsole import ainput

# if __name__ == '__main__':
IpAddress = '0.0.0.0' 
PORT = '8080'
clientName = 'Client1'
roomName = 'room'
messageToSend = ''
sio = AsyncClient()
FullIp = 'http://' IpAddress ':' PORT

@sio.event
async def connect():
    print('Connected to sever')
    await sio.emit('join_chat', {'room': roomName,'name': clientName})

@sio.event
async def get_message(message):
        if clientName == message['from']:
            pass
        else:
            print(message['message'])

async def send_message(msg): #Pass param in this function
    # while True:
        await asyncio.sleep(0.1)
        messageToSend = await ainput() # Instead of await ainput(), assign the param
        await sio.emit('send_chat_room', {'message': messageToSend,'name': clientName, 'room': roomName})

async def connectToServer():
    await sio.connect(FullIp)
    await sio.wait()

async def main(IpAddress):
        await asyncio.gather(
    connectToServer(),
    send_message("Hey")
    )

loop = asyncio.get_event_loop()
loop.run_until_complete(main(FullIp))

Client_led_disp.py

from socketio import AsyncClient
import asyncio
from json import dumps
from aioconsole import ainput
import GPIO_read
import usb_serial

if __name__ == '__main__':
    IpAddress = '0.0.0.0'
    PORT = '8080'
    clientName = 'Electronics'
    roomName = 'room'
    messageToSend = ''
    sio = AsyncClient()
    FullIp = 'http://' IpAddress ':' PORT

    @sio.event
    async def connect():
        print('Connected to sever')
        await sio.emit('join_chat', {'room': roomName, 'name': clientName})

    @sio.event
    async def get_message(message):
        if clientName == message['from']:
            pass
        else:
            if "Client1" == message['from']:
                usb_serial.print_on_display(message['message'])
            #print(message['from'])

    async def send_message():
        while True:
            await asyncio.sleep(5)
            messageToSend = dumps(GPIO_read.get_control_code())
            await sio.emit('send_chat_room', {'message': messageToSend, 'name': clientName, 'room': roomName})

    async def connectToServer():
        await sio.connect(FullIp)
        await sio.wait()

    async def main(IpAddress):
        await asyncio.gather(
            connectToServer(),
            send_message()
        )

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(FullIp))

CodePudding user response:

I think the GPIO_read.get_control_code() blocks the event loop. Which makes you are not able to receive new message anymore.

You can try to put this function into thread pool executor to avoid this problem. You can use asyncio.loop.run_in_executor in your condition.

  • Related