使用python asyncio.create_server实例提示用户输入

时间:2015-03-16 16:24:25

标签: python python-asyncio

我正在学习python 3 asyncio库,我遇到了一个小问题。我正在尝试从python文档调整EchoServer示例以提示用户输入,而不是仅回显客户端发送的内容。

我认为这就像添加对input()的调用一样简单,但当然input()会阻塞,直到有用户输入导致问题。

理想情况下,即使服务器没有“说”,我也希望继续从客户端接收数据。有点像聊天客户端,每个连接与服务器聊天。我希望能够切换到每个单独的连接,并根据需要从stdin发送输入。几乎就像一个P2P聊天客户端。

考虑以下修改过的EchoServer代码:

import asyncio

class EchoServerClientProtocol(asyncio.Protocol):
    def connection_made(self, transport):
        peername = transport.get_extra_info('peername')
        print('Connection from {}'.format(peername))
        self.transport = transport

    def data_received(self, data):
        message = data.decode()
        print('Data received: {!r}'.format(message))

        reply = input()
        print('Send: {!r}'.format(reply))
        self.transport.write(reply.encode())

        #print('Close the client socket')
        #self.transport.close()

loop = asyncio.get_event_loop()
# Each client connection will create a new protocol instance
coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)

# Serve requests until CTRL+c is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

我如何在服务器端获取输入格式stdin,并指定在连接客户端仍然接收输入的同时将其发送到哪个连接?

1 个答案:

答案 0 :(得分:10)

您可以使用loop.add_reader安排回调在sys.stdin上有数据时运行,然后使用asyncio.Queue将收到的标准输入数据传递到data_received方法:

import sys
import asyncio


def got_stdin_data(q):
    asyncio.async(q.put(sys.stdin.readline()))

class EchoServerClientProtocol(asyncio.Protocol):
   def connection_made(self, transport):
       peername = transport.get_extra_info('peername')
       print('Connection from {}'.format(peername))
       self.transport = transport

   def data_received(self, data):
       message = data.decode()
       print('Data received: {!r}'.format(message))
       fut = asyncio.async(q.get())
       fut.add_done_callback(self.write_reply)

   def write_reply(self, fut):
       reply = fut.result()
       print('Send: {!r}'.format(reply))
       self.transport.write(reply.encode())

       #print('Close the client socket')
       #self.transport.close()

q = asyncio.Queue()
loop = asyncio.get_event_loop()
loop.add_reader(sys.stdin, got_stdin_data, q)
# Each client connection will create a new protocol instance
coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)

# Serve requests until CTRL+c is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

唯一棘手的问题是我们如何调用Queue.put / Queue.get方法;它们都是协同程序,无法使用回调中的yield fromProtocol实例方法调用它们。相反,我们只使用asyncio.async使用事件循环计划它们,然后使用add_done_callback方法处理我们从get()调用中检索到的回复。