gpt4 book ai didi

python - 获取一个异步服务器来调用另一个服务器

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:18 24 4
gpt4 key购买 nike

所以我修改了 asyncio 客户端和服务器的示例(可在此处找到: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()

servertwo.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。部分工作;客户端确实调用了 serverone,而 serverone 确实调用了 servertwo,但随后 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

最佳答案

您可以使用 asyncio.async安排 create_connection 返回的协程由事件循环运行,然后使用 asyncio.Futureadd_done_callback 方法(更具体地说, 一个 asyncio.Task ),一旦协程完成,async 返回以关闭循环:

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()

关于python - 获取一个异步服务器来调用另一个服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25650025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com