Python 异步下载

def async_down_image(urls_dict, save_dirpath):
    async def down_image(client, url, save_path):
        root, name = os.path.split(save_path)
        if os.path.exists(save_path):
            return True
        try_count = 0
        while try_count < 3:
            try:
                async with client.get(url) as resp:
                    if resp.status == 200:
                        content = await resp.read()
                        print(f"Downloading {name}")
                        with open(save_path, 'wb') as fp:
                            fp.write(content)
                        return True
                    else:
                        try_count += 1
                        await asyncio.sleep(try_count)
            except:
                try_count += 1
                await asyncio.sleep(try_count)
        return False

    async def main(urls):
        async with aiohttp.ClientSession() as client:
            tasks = []
            for filename, url in urls.items():
                save_path = os.path.join(save_dirpath, filename)
                tasks.append(
                    asyncio.create_task(down_image(client, url, save_path=save_path))
                )
            await asyncio.wait(tasks)

    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    asyncio.run(main(urls_dict))

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.