2

所以我修改了异步客户端和服务器的示例(在这里找到:https ://docs.python.org/3/library/asyncio-protocol.html#protocol-example-tcp-echo-server-and-client )和我想要的只是让client.py调用serverone.py,然后调用servertwo.py。

客户端.py

#!/usr/bin/env python3.4
import asyncio

class EchoClient(asyncio.Protocol):
    message = 'This is the Client'

    def connection_made(self, transport):
        transport.write(self.message.encode())

    def data_received(self, data):
        print('data received: {}'.format(data.decode()))

    def connection_lost(self, exc):
        asyncio.get_event_loop().stop()

loop = asyncio.get_event_loop()
coro = loop.create_connection(EchoClient, '127.0.0.1', 8888)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()

serverone.py

#!/usr/bin/env python3.4

import asyncio

class EchoClient(asyncio.Protocol):
    message = 'Server One sending message'

    def connection_made(self, transport):
        transport.write(self.message.encode())

    def data_received(self, data):
        print('data received: {}'.format(data.decode()))

    def connection_lost(self, exc):
        asyncio.get_event_loop().stop()

class EchoServer(asyncio.Protocol):
    def connection_made(self, transport):
        peername = transport.get_extra_info('peername')
        self.transport = transport

    def data_received(self, data):
        loop = asyncio.get_event_loop()
        coro = loop.create_connection(EchoClient, '127.0.0.1', 8889)
        loop.run_until_complete(coro)
        # close the socket
        self.transport.close()
        loop.close()

loop = asyncio.get_event_loop()
coro = loop.create_server(EchoServer, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)
try:
    loop.run_forever()
except KeyboardInterrupt:
    print("exit")
finally:
    server.close()
    loop.close()

服务器二.py

#!/usr/bin/env python3.4

import asyncio

class EchoServer(asyncio.Protocol):
    def connection_made(self, transport):
        peername = transport.get_extra_info('peername')
        self.transport = transport

    def data_received(self, data):
        print('data received: {}'.format(data.decode()))
        self.transport.write(data)
        # close the socket
        self.transport.close()

loop = asyncio.get_event_loop()
coro = loop.create_server(EchoServer, '127.0.0.1', 8889)
server = loop.run_until_complete(coro)
try:
    loop.run_forever()
except KeyboardInterrupt:
    print("exit")
finally:
    server.close()
    loop.close()

我启动servertwo.pyserverone.py在终端中,然后调用client.py. 部分工作;客户端确实调用了调用 servertwo 的 serverone,但随后 serverone 失败并出现以下错误:

Exception in callback <bound method _SelectorSocketTransport._read_ready of <asyncio.selector_events._SelectorSocketTransport object at 0x7fbf4453b048>>()
handle: Handle(<bound method _SelectorSocketTransport._read_ready of <asyncio.selector_events._SelectorSocketTransport object at 0x7fbf4453b048>>, ())
Traceback (most recent call last):
  File "/usr/lib64/python3.4/asyncio/events.py", line 39, in _run
    self._callback(*self._args)
  File "/usr/lib64/python3.4/asyncio/selector_events.py", line 458, in _read_ready
    self._protocol.data_received(data)
  File "./serverone.py", line 25, in data_received
    loop.run_until_complete(coro)
  File "/usr/lib64/python3.4/asyncio/base_events.py", line 203, in run_until_complete
    self.run_forever()
  File "/usr/lib64/python3.4/asyncio/base_events.py", line 179, in run_forever
    raise RuntimeError('Event loop is running.')
RuntimeError: Event loop is running.

该文档没有涵盖很多奇怪的用例,所以我有点卡住了。我应该asyncio.async用来打电话吗?我是否正确地解决了这个问题?

我该如何解决或避免RuntimeError

4

2 回答 2

3

您可以使用asyncio.async调度返回的协程create_connection由事件循环运行,然后在协程完成后使用返回add_done_callbackasyncio.Future(更具体地说,an asyncio.Taskasync方法关闭循环:

class EchoServer(asyncio.Protocol):
    def connection_made(self, transport):
        peername = transport.get_extra_info('peername')
        self.transport = transport

    def data_received(self, data):
        loop = asyncio.get_event_loop()
        coro = loop.create_connection(EchoClient, '127.0.0.1', 8890)
        fut = asyncio.async(coro)
        fut.add_done_callback(self.shutdown)

    def shutdown(self, *args):
        self.transport.close()
        loop.stop()
于 2014-09-03T17:47:45.290 回答
0

替换asyncio.asyncasyncio.ensure_future,它将以赞成的答案运行。

于 2019-11-01T03:57:47.227 回答