小编Yur*_*yda的帖子

Python中的异步异常处理

我有以下代码使用asyncioaiohttp发出异步HTTP请求.

import sys
import asyncio
import aiohttp

@asyncio.coroutine
def get(url):
    try:
        print('GET %s' % url)
        resp = yield from aiohttp.request('GET', url)
    except Exception as e:
        raise Exception("%s has error '%s'" % (url, e))
    else:
        if resp.status >= 400:
            raise Exception("%s has error '%s: %s'" % (url, resp.status, resp.reason))

    return (yield from resp.text())

@asyncio.coroutine
def fill_data(run):
    url = 'http://www.google.com/%s' % run['name']
    run['data'] = yield from get(url)

def get_runs():
    runs = [ {'name': 'one'}, {'name': 'two'} ]
    loop …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-asyncio

38
推荐指数
2
解决办法
3万
查看次数

标签 统计

python ×1

python-3.x ×1

python-asyncio ×1