Python3异步-在自己的线程中运行两个服务器

时间:2018-11-04 08:43:11

标签: python python-3.5 python-multithreading python-asyncio

我的服务器类继承自BaseServer

class BaseServer(object):

    def __init__(self, host, port):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

        self.instance = asyncio.start_server(self.handle_connection, host = host, port = port)

    async def handle_connection(self, reader: StreamReader, writer: StreamWriter):
        pass

    def start(self):
        # wrapping coroutine into ensure_future to allow it to call from call_soon
        # wrapping into lambda to make it callable
        callback = asyncio.ensure_future(self.instance)
        self.loop.call_soon(lambda: callback)
        self.loop.run_forever()
        self.loop.close()

    def stop(self):
        self.loop.call_soon_threadsafe(self.loop.stop)

    @staticmethod
    def get_instance():
        return BaseServer(None, None)

我需要两个在自己的线程中运行的服务器来并行处理请求。 但是,当我尝试根据需要运行它们时,只有第一台服务器正在运行。下面是我的运行方式:

if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    async def run():
        pool = ThreadPoolExecutor(max_workers=cpu_count())

        await loop.run_in_executor(pool, Server1.get_instance().start)
        await loop.run_in_executor(pool, Server2.get_instance().start)

    loop.run_until_complete(run())
  1. 我在做什么错?如何在自己的线程中运行每个服务器?
  2. asyncio.set_event_loopdef __init__呼叫时,我遇到下一个错误:
  

RuntimeError:线程'Thread-1'中没有当前事件循环。

但是,如果我从asyncio.set_event_loop中删除def __init__并将其移至def start,错误就会消失。为什么会这样?

1 个答案:

答案 0 :(得分:3)

关于OP的评论:

  

但是在同一循环块中通过run_until_complete运行两台服务器,其中一台服务器循环处理向另一台服务器的请求,不是吗?如何正确运行两台服务器?

这是来自python 3.5的asyncio文档中TCP server example的修改版本:

# Start server 1
coro1 = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop)
server1 = loop.run_until_complete(coro1)
print('Serving 1 on {}'.format(server1.sockets[0].getsockname()))

# Start server 2
coro2 = asyncio.start_server(handle_echo, '127.0.0.1', 8889, loop=loop)
server2 = loop.run_until_complete(coro2)
print('Serving 2 on {}'.format(server2.sockets[0].getsockname()))

# Serve requests until Ctrl+C is pressed
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

# Close the servers
server1.close()
loop.run_until_complete(server1.wait_closed())
server2.close()
loop.run_until_complete(server2.wait_closed())

# Close the loop
loop.close()

请注意,使用python 3.7对asyncio的添加,它看起来像much nicer

async def main():
    server1 = await asyncio.start_server(
        handle_echo, '127.0.0.1', 8888)

    addr1 = server1.sockets[0].getsockname()
    print(f'Serving 1 on {addr1}')

    server2 = await asyncio.start_server(
        handle_echo, '127.0.0.1', 8889)

    addr2 = server2.sockets[0].getsockname()
    print(f'Serving 2 on {addr2}')

    async with server1, server2:
        await asyncio.gather(
            server1.serve_forever(), server2.serve_forever())

asyncio.run(main())