使用asyncio进行多次调用并将结果添加到字典

时间:2015-08-22 22:22:46

标签: python python-3.x python-asyncio

我无法绕过Python 3的Asyncio库。我有一个zipcodes列表,我正在尝试对API进行异步调用,以使每个zipcodes对应城市和州。我可以使用for循环顺序成功完成,但我希望在大型邮政编码列表的情况下使其更快。

这是我原作的一个例子

import urllib.request, json

zips = ['90210', '60647']

def get_cities(zipcodes):
    zip_cities = dict()
    for idx, zipcode in enumerate(zipcodes):
        url = 'http://maps.googleapis.com/maps/api/geocode/json?address='+zipcode+'&sensor=true'
        response = urllib.request.urlopen(url)
        string = response.read().decode('utf-8')
        data = json.loads(string)
        city = data['results'][0]['address_components'][1]['long_name']
        state = data['results'][0]['address_components'][3]['long_name']
        zip_cities.update({idx: [zipcode, city, state]})
    return zip_cities

results = get_cities(zips)
print(results)
# returns {0: ['90210', 'Beverly Hills', 'California'],
#          1: ['60647', 'Chicago', 'Illinois']}

这是我尝试使其异步的非功能性尝试

import asyncio
import urllib.request, json

zips = ['90210', '60647']
zip_cities = dict()

@asyncio.coroutine
def get_cities(zipcodes):
    url = 'http://maps.googleapis.com/maps/api/geocode/json?address='+zipcode+'&sensor=true'
    response = urllib.request.urlopen(url)
    string = response.read().decode('utf-8')
    data = json.loads(string)
    city = data['results'][0]['address_components'][1]['long_name']
    state = data['results'][0]['address_components'][3]['long_name']
    zip_cities.update({idx: [zipcode, city, state]})

loop = asyncio.get_event_loop()
loop.run_until_complete([get_cities(zip) for zip in zips])
loop.close()
print(zip_cities) # doesnt work

非常感谢任何帮助。我在网上遇到的所有教程似乎都让我头脑发热。

注意:我看过一些使用aiohttp的例子。如果可能的话,我希望坚持使用本机Python 3库。

2 个答案:

答案 0 :(得分:13)

如果使用urllib来执行HTTP请求,则无法获得任何并发性,因为它是一个同步库。在urllib中包含调用coroutine的函数不会改变该函数。您必须使用集成到asyncio的异步HTTP客户端,例如aiohttp

import asyncio
import json
import aiohttp

zips = ['90210', '60647']
zip_cities = dict()

@asyncio.coroutine
def get_cities(zipcode,idx):
    url = 'https://maps.googleapis.com/maps/api/geocode/json?key=abcdfg&address='+zipcode+'&sensor=true'
    response = yield from aiohttp.request('get', url)
    string = (yield from response.read()).decode('utf-8')
    data = json.loads(string)
    print(data)
    city = data['results'][0]['address_components'][1]['long_name']
    state = data['results'][0]['address_components'][3]['long_name']
    zip_cities.update({idx: [zipcode, city, state]})

if __name__ == "__main__":        
    loop = asyncio.get_event_loop()
    tasks = [asyncio.async(get_cities(z, i)) for i, z in enumerate(zips)]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    print(zip_cities)

我知道你更喜欢只使用stdlib,但asyncio库不包含HTTP客户端,所以你必须基本上重新实现aiohttp的部分来重新创建功能它的提供。我想另一个选择是在后台线程中进行urllib调用,这样它们就不会阻塞事件循环,但是当aiohttp可用时它会有些愚蠢(和首先违背了使用asyncio的目的):

import asyncio
import json
import urllib.request
from concurrent.futures import ThreadPoolExecutor

zips = ['90210', '60647']
zip_cities = dict()

@asyncio.coroutine
def get_cities(zipcode,idx):
    url = 'https://maps.googleapis.com/maps/api/geocode/json?key=abcdfg&address='+zipcode+'&sensor=true'
    response = yield from loop.run_in_executor(executor, urllib.request.urlopen, url)
    string = response.read().decode('utf-8')
    data = json.loads(string)
    print(data)
    city = data['results'][0]['address_components'][1]['long_name']
    state = data['results'][0]['address_components'][3]['long_name']
    zip_cities.update({idx: [zipcode, city, state]})

if __name__ == "__main__":
    executor = ThreadPoolExecutor(10)
    loop = asyncio.get_event_loop()
    tasks = [asyncio.async(get_cities(z, i)) for i, z in enumerate(zips)]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    print(zip_cities)

答案 1 :(得分:3)

asyncio做得不多,但self.EURToUSD = json[6]['EUR_USD']应该是你需要的,你显然也必须改变你的函数作为参数的内容,并根据docs使用asyncio.get_event_loop()

asyncio.wait(tasks)

我没有> = 3.4.4所以我不得不使用zips = ['90210', '60647'] zip_cities = dict() @asyncio.coroutine def get_cities(zipcode): url = 'https://maps.googleapis.com/maps/api/geocode/json?key=abcdefg&address='+zipcode+'&sensor=true' fut = loop.run_in_executor(None,urllib.request.urlopen, url) response = yield from fut string = response.read().decode('utf-8') data = json.loads(string) city = data['results'][0]['address_components'][1]['long_name'] state = data['results'][0]['address_components'][3]['long_name'] zip_cities.update({idx: [zipcode, city, state]}) loop = asyncio.get_event_loop() tasks = [asyncio.async(get_cities(z, i)) for i, z in enumerate(zips)] loop.run_until_complete(asyncio.wait(tasks)) loop.close() print(zip_cities) # doesnt work {0: ['90210', 'Beverly Hills', 'California'], 1: ['60647', 'Chicago', 'Illinois']} 代替asyncio.async

或者更改逻辑并从任务中的task.result创建dict:

asyncio.ensure_future

如果你正在查看外部模块,还有一个port of requests可以与asyncio一起使用。