Python asyncio无法正常工作

时间:2018-05-25 09:20:41

标签: python python-asyncio

我想使用asyncio来获取一组图像的大小。

传递一组网址,我希望得到三个值,android:focusableInTouchMode="true"

然而,我无法得到任何东西。

我的代码有什么问题吗?

(x, y, channel)

1 个答案:

答案 0 :(得分:2)

试试这个:

from PIL import Image
from io import BytesIO
import numpy as np
import asyncio
import concurrent.futures
import requests

def get_image_shape(path):
    try:
        img = Image.open(BytesIO(requests.get(path).content))
        arr = np.array(img, dtype = np.uint8)
        return arr.shape
    except:
        return (0,0,0)

async def main(url_list):
    loop = asyncio.get_event_loop()
    futures = [
        loop.run_in_executor(
            None, 
            get_image_shape, 
            url
        )
        for url in url_list]
    return [response for response in await asyncio.gather(*futures)]

loop = asyncio.get_event_loop()
response = loop.run_until_complete(main(url_list))