为什么这个Python 3异步IO客户端只在Python进程中断后才发送字节?
为什么我的客户在代码行超时:为什么这个Python 3异步IO客户端只在Python进程中断后才发送字节?,python,client-server,python-3.4,python-asyncio,Python,Client Server,Python 3.4,Python Asyncio,为什么我的客户在代码行超时: response=yield from asyncio.wait_for(reader.read(),timeout=3.0) 而不是收到回信 服务器只在客户端超时后注册它从该客户端收到一条消息,我实际上发出了一个ctrl+d来终止Python解释器 client.py 问题可能是您正在服务器端(以及客户端)调用reader.read(),这将一直阻止,直到服务器发送EOF。但你可能没有这样做——你只是发送了一些字节,并保持连接打开 相反,您需要调用readlin
response=yield from asyncio.wait_for(reader.read(),timeout=3.0)
而不是收到回信
服务器只在客户端超时后注册它从该客户端收到一条消息,我实际上发出了一个ctrl+d
来终止Python解释器
client.py
问题可能是您正在服务器端(以及客户端)调用
reader.read()
,这将一直阻止,直到服务器发送EOF
。但你可能没有这样做——你只是发送了一些字节,并保持连接打开
相反,您需要调用readline()
并确保将b'\n'
附加到您的mess有效负载,调用read
以限制等待的字节数,或者在写入消息有效负载后调用,假设您不再计划使用writer
。使用readline()
或write\eof
可能是最安全的选择。下面是一个完整的示例,演示如何使用readline()
:
client.py
server.py
下面是使用write_eof()
的更改:
client.py
server.py
@asyncio.coroutine
def test_connection(host, port):
# Get the streams for the socket
print('Starting client connection')
reader, writer = yield from asyncio.open_connection(host, port)
message = 'Test Message'.encode('ascii')
# Start communication
print('Sending message to {}:{} - {}'.format(host, port, message))
writer.write(message)
print('Waiting 3 sec for response...')
response = yield from asyncio.wait_for(reader.read(), timeout=3.0)
print('Got response: {}'.format(response.decode('ascii')))
writer.close()
def run():
loop = asyncio.get_event_loop()
task = asyncio.async(test_connection())
loop.run_until_complete(task)
loop.close()
import asyncio
@asyncio.coroutine
def test_connection(host, port):
# Get the streams for the socket
print('Starting client connection')
reader, writer = yield from asyncio.open_connection(host, port)
message = 'Test Message\n'.encode('ascii')
# Start communication
print('Sending message to {}:{} - {}'.format(host, port, message))
writer.write(message)
print('Waiting 3 sec for response...')
response = yield from asyncio.wait_for(reader.readline(), timeout=5.0)
print('Got response: {}'.format(response.decode('ascii')))
writer.close()
def run():
loop = asyncio.get_event_loop()
task = asyncio.async(test_connection('localhost', 5000))
loop.run_until_complete(task)
loop.close()
run()
import asyncio
@asyncio.coroutine
def got_connection(reader, writer):
msg = yield from reader.readline()
message = 'another Test Message\n'.encode('ascii')
print('Sending message to {}:{} - {}'.format('localhost', 5000, message))
writer.write(message)
def run():
loop = asyncio.get_event_loop()
server = loop.run_until_complete(asyncio.start_server(
got_connection, 'localhost', 5000))
loop.run_until_complete(server.wait_closed())
loop.close()
run()
@asyncio.coroutine
def test_connection(host, port):
# Get the streams for the socket
print('Starting client connection')
reader, writer = yield from asyncio.open_connection(host, port)
message = 'Test Message'.encode('ascii')
# Start communication
print('Sending message to {}:{} - {}'.format(host, port, message))
writer.write(message)
writer.write_eof()
print('Waiting 3 sec for response...')
response = yield from asyncio.wait_for(reader.read(), timeout=5.0)
print('Got response: {}'.format(response.decode('ascii')))
writer.close()
@asyncio.coroutine
def got_connection(reader, writer):
msg = yield from reader.read()
message = 'another Test Message'.encode('ascii')
print('Sending message to {}:{} - {}'.format('localhost', 5000, message))
writer.write(message)
writer.write_eof()