Created
October 13, 2015 18:52
-
-
Save pathcl/df9bb68cda8e2e87d4a3 to your computer and use it in GitHub Desktop.
aiohttp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio | |
| import aiohttp | |
| @asyncio.coroutine | |
| def get_status(url): | |
| code = '000' | |
| try: | |
| res = yield from asyncio.wait_for(aiohttp.request('GET', url), 4) | |
| code = res.status | |
| res.close() | |
| except Exception as e: | |
| print(e) | |
| print(code) | |
| if __name__ == "__main__": | |
| urls = ['http://someurl/index.html'] * 10000 | |
| coros = [asyncio.Task(get_status(url)) for url in urls] | |
| loop = asyncio.get_event_loop() | |
| executor = concurrent.futures.ThreadPoolExecutor(5) | |
| loop.set_default_executor(executor) | |
| executor.shutdown(wait=True) | |
| loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment