gpt4 book ai didi

python 3.6 协程从未被期待

转载 作者:行者123 更新时间:2023-12-01 03:08:20 24 4
gpt4 key购买 nike

因此,当我运行我的程序并使用 echo 客户端连接到它时,它会给我这个错误。

Starting server
Serving on ('127.0.0.1', 8881)
Exception in callback UVTransport._call_connection_made
handle: <Handle UVTransport._call_connection_made>
Traceback (most recent call last):
File "uvloop/cbhandles.pyx", line 52, in uvloop.loop.Handle._run (uvloop/loop.c:48414)
File "uvloop/handles/tcp.pyx", line 141, in uvloop.loop.TCPTransport._call_connection_made (uvloop/loop.c:80488)
File "uvloop/handles/basetransport.pyx", line 140, in uvloop.loop.UVBaseTransport._call_connection_made (uvloop/loop.c:65774)
File "uvloop/handles/basetransport.pyx", line 137, in uvloop.loop.UVBaseTransport._call_connection_made (uvloop/loop.c:65671)
AttributeError: 'coroutine' object has no attribute 'connection_made'
/home/kenton/Programming/bridal/bridal-middle/middle/lib/server.py:16:RuntimeWarning: coroutine 'handle_request' was never awaited
loop.run_forever()

据我所知,我已经拥有了所有应该等待的事情。这是代码:

class Server:

def __init__(self, port):
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
server = loop.run_until_complete(self.init(loop))

print("Serving on {}".format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
print("\rclosing the server")
pass

server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

async def init(self, loop):
server = await loop.create_server(self.handle_request, '127.0.0.1', 8881)
return server

async def handle_request(self):
print(datetime.datetime.now())
reader = asyncio.StreamReader()
writer = asyncio.StreamWriter()
data = await reader.read(100)
message = data.decode()
addr = writer.get_extra_info('peername')
code = message.partition('-')
if code[0].startswith("1") or code[0].startswith("5"):
accounts = lib.settings.database.accounts
if code[0] == "101":
result = await self.login_101(code, accounts, writer)
if code[0] == "501":
result = await accounts.find_one({"username":code[2]})
print("looking up", code[0])
#code logic to keep asking for a valid username if one exists
if result is None:
username = code[2]
print(username, " does not exist. Creating")
writer.write(b"0")
await writer.drain()
data = await reader.read(100)
message = data.decode()
code = message.partition('-')
post = {"username":username,"password":code[0],"email":code[2]}
post_id = await accounts.insert_one(post).inserted_id
writer.write(b(post_id))
await writer.drain()
print("Closed the client socket")
writer.close()
print(datetime.datetime.now())

最佳答案

关于您的错误消息,实际错误是:

AttributeError: 'coroutine' object has no attribute 'connection_made'

下面的行只是一个警告(RuntimeWarning: coroutine 'handle_request' was never waitingd)。

您可能正在混合 asyncio.start_serverloop.create_server()

loop.create_server()的第一个参数是 protocol_factory ,它是一个返回 Protocol 实例的可调用函数(而不是上面代码中的协程):

import asyncio

class MyProtocol(asyncio.Protocol):
def connection_made(self, transport):
print("Connection made", transport)

def data_received(self, data):
print("Data received", data)

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

查看完整的回显服务器示例 here .

关于python 3.6 协程从未被期待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43124340/

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