Python 我在使用sanic和aiohttp时遇到一个错误[TypeError:';协同程序';对象不可编辑]

Python 我在使用sanic和aiohttp时遇到一个错误[TypeError:';协同程序';对象不可编辑],python,python-3.x,python-asyncio,aiohttp,sanic,Python,Python 3.x,Python Asyncio,Aiohttp,Sanic,我使用Sanic构建了一个小型web服务器,以下是其中一个视图: @app.route("/query_video/", methods=["GET"]) async def video_query_views(request: Request): keyword = request.args.get("keyword", None) page = request.args.get("page", None) order = request.args.get("order

我使用Sanic构建了一个小型web服务器,以下是其中一个视图:

@app.route("/query_video/", methods=["GET"])
async def video_query_views(request: Request):
    keyword = request.args.get("keyword", None)
    page = request.args.get("page", None)
    order = request.args.get("order", None)
    query_params = dict()
    if keyword:
        query_params['keyword'] = keyword
    else:
        return json("keyword can't be empty", status=403)
    if page:
        query_params['page'] = page
    if order:
        query_params['order'] = order
    try:
        rest = dict()
        rest['bilibili'] = await get_bilibili_query(**query_params)
        rest['qq'] = await get_v_qq_query(**query_params)
        rest['youku'] = await get_youku_query(**query_params)
        rest['iqiyi'] = await get_iqiyi_query(**query_params)
        rest['mg'] = await get_mgtv_query(**query_params)
    except Exception as e:
        print(traceback.format_exc())
        return json(str(e), status=403)
    return json(rest)
在get_bilibili_query,get_v_qq_query,…,函数get_bilibili_query的代码中都出现了错误:

async def get_bilibili_query(keyword, page=1, order='tolalrank'):
    base_url = "https://search.bilibili.com/all"
    query_params = {
        "keyword": keyword,
        "page": page,
        "order": order,
    }
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, url=base_url, params=query_params)
    html = etree.HTML(html)
    lis = html.xpath("//li[@class='video matrix ']")
    rst = list()
    for li in lis:
        try:
            a = li.xpath('a')[0]
            img = a.xpath('div/img')[0]

            time_len = a.xpath('div/span/text()')[0].strip()
            title = a.xpath("@title")[0].strip()
            origin_url = urljoin(base_url, a.xpath('@href')[0].strip())
            img_url = urljoin(base_url, img.xpath("@data-src")[0].strip())
            rst.append({
                'origin_url': origin_url,
                'time_len': time_len,
                'img_url': img_url,
                'title': title
            })
        except Exception:
            continue
    else:
        return rst
函数获取:

async def fetch(session, url, **kwargs):
    with async_timeout.timeout(10):
        async with session.get(url, **kwargs, verify_ssl=False) as response:
            return await response.text()
只需使用aiohttp.ClientSession方法[get]

错误详细信息:

Traceback (most recent call last):
  File "/Users/angelo/PycharmProjects/pp3/s1.py", line 51, in video_query_views
    rest['iqiyi'] = await get_iqiyi_query(**query_params)
  File "/Users/angelo/PycharmProjects/pp3/sp.py", line 25, in get_bilibili_query
    html = await fetch(session, url=base_url, params=query_params)
  File "/Users/angelo/PycharmProjects/pp3/sp.py", line 13, in fetch
    async with session.get(url, **kwargs, verify_ssl=False) as response:
  File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/client.py", line 690, in __aenter__
    self._resp = yield from self._coro
  File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/client.py", line 267, in _request
    conn = yield from self._connector.connect(req)
  File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 402, in connect
    proto = yield from self._create_connection(req)
  File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 748, in _create_connection
    _, proto = yield from self._create_direct_connection(req)
  File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 831, in _create_direct_connection
    req=req, client_error=client_error)
  File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 796, in _wrap_create_connection
    return (yield from self._loop.create_connection(*args, **kwargs))
TypeError: 'coroutine' object is not iterable
但是,如果我直接运行该函数,则不会发生错误!
请帮忙

这是uvloop中的一个bug,实际上是Cython:


禁用uvloop的相同代码可以正常工作。

您可以创建一个最小的自包含示例来重现错误吗?不应该
使用异步\u超时。超时(10)
应该
与…异步。