Python socket.io 邮件在转储后被阻止
Python socket.io messages getting blocked after dumps
我正在尝试在两个客户端之间进行双向通信,但是一旦转储,我就无法再从客户端文件发送任何消息
发送消息是我认为阻止获取部分的内容。有人知道怎么修这个东西吗?提前致谢
(我可以从客户端向 client_led 发送一次消息,但之后就不行了)
@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})
完整脚本
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))
我认为 GPIO_read.get_control_code()
阻塞了事件循环。
这使您无法再收到新消息。
你可以尝试把这个函数放到线程池执行器中来避免这个问题。你可以在你的条件下使用asyncio.loop.run_in_executor
。
我正在尝试在两个客户端之间进行双向通信,但是一旦转储,我就无法再从客户端文件发送任何消息 发送消息是我认为阻止获取部分的内容。有人知道怎么修这个东西吗?提前致谢
(我可以从客户端向 client_led 发送一次消息,但之后就不行了)
@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})
完整脚本
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))
我认为 GPIO_read.get_control_code()
阻塞了事件循环。
这使您无法再收到新消息。
你可以尝试把这个函数放到线程池执行器中来避免这个问题。你可以在你的条件下使用asyncio.loop.run_in_executor
。