如何处理运行时警告:从未等待协同程序“新帐户”

2024-04-26 17:20:12 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我启动web.py并转到localhost:8080/register时,就会出现此错误。 这是Flash游戏的一部分

web.py:75: RuntimeWarning: coroutine 'new_account' was never awaited
  uid, password = utils.bot_common.new_account(app["redis"])
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Error handling request
Traceback (most recent call last):
  File "/root/.local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line418, in start
    resp = await task
  File "/root/.local/lib/python3.7/site-packages/aiohttp/web_app.py", line 458,in _handle
    resp = await handler(request)
  File "/root/.local/lib/python3.7/site-packages/aiohttp/web_middlewares.py", lne 119, in impl
    return await handler(request)
  File "/root/.local/lib/python3.7/site-packages/aiohttp_session/__init__.py", ine 154, in factory
    response = await handler(request)
  File "web.py", line 75, in register
    uid, password = utils.bot_common.new_account(app["redis"])
TypeError: cannot unpack non-iterable coroutine object

上述错误指向web.py第75行:

@routes.get("/register")
async def register(request):
    if not registation:
        return web.Response(text="Регистрация отключена")
    uid, password =  utils.bot_common.new_account(app["redis"])
    return web.Response(text=f"Аккаунт создан, ваш логин - {uid}, "
                             f"пароль - {password}")

有关bot\u commony.py注册bot的更多信息:

import string
import random
def random_string(string_length=20):
    letters = string.ascii_letters
    return ''.join(random.choice(letters) for i in range(string_length))

async def new_account(redis):
    await redis.incr("uids")
    uid = await redis.get("uids")
    pipe = redis.pipeline()
    pipe.set(f"uid:{uid}:lvt", 0)
    pipe.sadd(f"rooms:{uid}", "livingroom")
    pipe.rpush(f"rooms:{uid}:livingroom", "#livingRoom", 1)
    for i in range(1, 6):
        pipe.sadd(f"rooms:{uid}", f"room{i}")
        pipe.rpush(f"rooms:{uid}:room{i}", f"Комната {i}", 2)
    await pipe.execute()
    return uid

Tags: inpyredisregisterwebnewuidstring
1条回答
网友
1楼 · 发布于 2024-04-26 17:20:12

new_account返回一个协程,因此

TypeError: cannot unpack non-iterable coroutine object

需要等待(或包装在任务中)协同路由。要修复这个特定的TypeError,您需要将代码更新为

uid, password = await untold.bot_common.new_account(app["redis"])

一旦您进行了此更改,我怀疑您将获得一个新的TypeError

TypeError: cannot unpack non-iterable int object

这是因为new_account返回单个值:uid。基于await redis.incr("uids"),看起来您有一个整数,而不是两个字符的字符串或包含两个值的容器。您需要将register中的行更改为

uid = await untold.bot_common.new_account(app["redis"])

或者您需要更改new_account以返回多个值

return uid, "some password"

相关问题 更多 >