- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
考虑以下代码:
from aiohttp_mako import template
def authorize():
def wrapper(func):
@asyncio.coroutine
@functools.wraps(func)
def wrapped(*args):
allowed = # Some auth stuff
if not allowed:
return HTTPUnauthorized()
return func()
return wrapped
return wrapper
@authorize()
@template('admin.mak')
async def admin(request):
return dict(ok=True)
我希望 authorize()
' wrapper
得到template
装饰器作为其 func
这样我就可以返回 Response
它在我的 authorize
中生成装饰器。但是authorize()
' wrapper
需要 admin()
协程作为 func
最后是
File "/Users/etemin/virtualenvs/onlinelux/lib/python3.5/site-packages/aiohttp/web.py", line 306, in _handle
resp = yield from handler(request)
File "/Users/etemin/virtualenvs/onlinelux/lib/python3.5/site-packages/aiohttp_session/__init__.py", line 134, in middleware
raise RuntimeError("Expect response, not {!r}", type(response))
RuntimeError: ('Expect response, not {!r}', <class 'generator'>)
因为它试图返回协程。我应该如何返回 template
装饰器?
最佳答案
您已经包装了一个协同例程,因此您需要等待该协同例程(从中产生):
def authorize():
def wrapper(func):
@asyncio.coroutine
@functools.wraps(func)
def wrapped(*args):
allowed = # Some auth stuff
if not allowed:
return HTTPUnauthorized()
return yield from func()
return wrapped
return wrapper
因为您已经在使用 async
/await
语法,所以我在这里也只使用它而不使用 @asyncio.coroutine
:
def authorize():
async def wrapper(func):
@functools.wraps(func)
async def wrapped(*args):
allowed = # Some auth stuff
if not allowed:
return HTTPUnauthorized()
return await func()
return wrapped
return wrapper
请注意,我在那里等待 func()
,并返回了结果。
关于python - aiohttp:装饰器序列链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44848486/
我想要一个类似于 django runserver 所做的重新加载。 如果我更改 python 文件,我希望应用程序重新加载。我已经安装了 aiohttp-devtools 并使用 adev runs
我在使用 RouteTableDef 时遇到问题。 有一些项目的路由结构如下: 1) 有文件route.py。 路线.py from aiohttp import web routes = web.R
我有一些代码对某些 API 进行请求序列。我想为所有人设置通用日志记录,我该如何设置? 假设我的代码是这样的 import aiohttp import asyncio async def fetch
您能否就以下方面提出建议? 在 localhost:8900 上有 aiohttp 服务器在运行 当我从 python 发出类似(使用 python2 模块请求)的请求时 requests.get("
每当我对使用 asyncio 和 aiohttp 访问的 API 执行超过 200 个请求时,我都会收到 aiohttp client_exception.ServerDisconnectedErro
在我正在开发的爬虫中。它使用 pycurl multi 发出请求。 如果我改用aiohttp,我可以期待什么样的效率提升? 怀疑让我怀疑潜在的改进,因为 python 有 GIL。大部分时间都花在等待
我在尝试使用 azure 测试聊天机器人时遇到一些问题: 我使用 github actions 在 azure web 应用程序上部署了我的机器人,一切都很顺利。但是当我尝试测试我的聊天机器人时,没有
我在尝试使用 azure 测试聊天机器人时遇到一些问题: 我使用 github actions 在 azure web 应用程序上部署了我的机器人,一切都很顺利。但是当我尝试测试我的聊天机器人时,没有
我想知道如何从 aiohttp post 方法获取当前的上传步骤。通常我会使用 get 方法在循环中拉取当前步骤,但如果主机不响应当前上传步骤,这将不起作用。那么有可能得到当前步骤吗?诸如“从 xx%
我目前正在用 aiohttp 做我的第一个“婴儿学步” (来自 requests 模块)。 我尝试稍微简化请求,这样我就不必在主模块中为每个请求使用上下文管理器。 因此我尝试了这个: async de
tl;dr:如何最大化可以并行发送的 http 请求数量? 我正在使用 aiohttp 库从多个网址获取数据。我正在测试它的性能,并且观察到该过程中的某个地方存在瓶颈,一次运行更多的网址并没有帮助。
目前我正在执行以下操作来获取当前正在运行的应用程序 async def handler(request): app = request.app 是否有其他方法来获取当前正在运行的应用程序?考虑
首先是代码: import random import asyncio from aiohttp import ClientSession import csv headers =[] def ext
我的 aiohttp 中间件获取函数作为参数,而不是已传递给路由的绑定(bind)方法。如何解释这种行为?如何避免这种情况? class AsyncHttpServer: def __init
我正在尝试在 aiohttp 处理程序中启动后台长时间任务: from aiohttp import web import time import asyncio async def one(requ
我正在测试 aiohttp 和 asyncio。我希望相同的事件循环具有套接字、http 服务器、http 客户端。 我正在使用此示例代码: @routes.get('/') async def he
#!/usr/bin/env python3.5 import asyncio import aiohttp url = "http://eniig.dk" async def main():
考虑以下代码: from aiohttp_mako import template def authorize(): def wrapper(func): @asyncio.c
我正在编写一个网络爬虫,它为许多不同的域运行并行提取。我想限制每秒向每个单独的域发出的请求数,但我不关心打开的连接总数或每秒的总请求数跨越所有领域。 我想最大限度地提高打开的连接数和每秒请求数,同时限
我需要将 sub_app 添加到 sub_app。这是我的代码 app = web.Application() subapp = web.Application() subapp.router.add
我是一名优秀的程序员,十分优秀!