【发布时间】:2020-06-18 18:45:50
【问题描述】:
我正在运行这个接收数据的服务器。但是我希望它每秒更新一次。这个 Asyncio 循环说它一直在运行,但它只接收一次数据。
我可以执行哪些循环来每 n 秒更新一次消息检索,我应该将这些循环放在哪里?我尝试过线程、For/While 循环等,但我可能把它们放在了错误的地方。
我该怎么办?
import asyncio
import websockets
import socket
UDP_IP = socket.gethostname()
UDP_PORT = 5225
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
#print(str(data))
x = 1
async def echo(websocket, path):
async for message in websocket:
await asyncio.sleep(1)
await websocket.send(str(data)) #FontWeight Value
print(bytes(data))
start_server = websockets.serve(echo, "localhost", 9090)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
#loop.run_forever(start_server)
【问题讨论】:
标签: python websocket udp python-asyncio