我编写简单的udp客户端程序,但无法正确接收数据报 我的代码如下。
import asyncio
class EchoClientProtocol:
def __init__(self, message, loop):
self.message = message
self.loop = loop
self.transport = None
def connection_made(self, transport):
self.transport = transport
print('Send:', self.message)
self.transport.sendto(self.message.encode())
def datagram_received(self, data, addr):
print('Received:', data.decode())
async def sendChar(transport, msg):
print('send: ', msg)
transport.sendto(msg.encode())
if __name__ == '__main__':
loop = asyncio.get_event_loop()
message = 'Hello World!'
connect = loop.create_datagram_endpoint(
lambda: EchoClientProtocol(message, loop),
remote_addr=('127.0.0.1', 9999)
)
transport, protocol = loop.run_until_complete(connect)
while (True):
try:
ch = input()
except KeyboardInterrupt:
break
loop.run_until_complete(sendChar(transport, ch))
loop.run_forever()
transport.close()
loop.close()
我编写了asyncio文档中发布的UDP echo服务器协议程序 (https://docs.python.org/3/library/asyncio-protocol.html#udp-echo-server-protocol)
运行这些程序,我认为结果是这样的。
Send: Hello World!
Received: Hello World!
1
send: 1
Received: 1
2
send: 2
Received: 2
但结果是这样。
Send: Hello World!
1
send: 1
Received: Hello World!
2
send: 2
Received: 1
为什么结果会发生变化? 我搜索了asyncio模块,但我无法解决这个问题。
答案 0 :(得分:0)
这是因为while True
循环在下一次发送呼叫 - input
阻塞之前不会将控制权交还给ioloop。只需添加一些异步睡眠,因此ioloop可以处理接收数据的事件
while (True):
try:
loop.run_until_complete(asyncio.sleep(0.001))
ch = input()
except KeyboardInterrupt:
break
loop.run_until_complete(sendChar(transport, ch))
由于input
也会在这里阻止,所以最好将它移到单独的线程中:
import threading
import asyncio
class EchoClientProtocol:
def __init__(self, message, loop):
self.message = message
self.loop = loop
self.transport = None
def connection_made(self, transport):
self.transport = transport
print('Send:', self.message)
self.transport.sendto(self.message.encode())
def datagram_received(self, data, addr):
print('Received:', data.decode())
def error_received(self, data, addr=None):
raise data
async def sendChar(transport, msg):
print('send: ', msg)
transport.sendto(msg.encode())
user_input = [None]
# spawn a new thread to wait for input
def get_user_input(user_input_ref):
while True:
user_input_ref[0] = input()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
message = 'Hello World!'
connect = loop.create_datagram_endpoint(
lambda: EchoClientProtocol(message, loop),
remote_addr=('127.0.0.1', 9999)
)
transport, protocol = loop.run_until_complete(connect)
input_thread = threading.Thread(target=get_user_input, args=(user_input,))
input_thread.daemon = True
input_thread.start()
while (True):
if user_input[0] is not None:
loop.run_until_complete(sendChar(transport, user_input[0]))
user_input[0] = None
loop.run_until_complete(asyncio.sleep(1))
loop.run_forever()
transport.close()
loop.close()
我已将您的代码与waiting for user input in separate thread混合,当然要重构:)