(python3.5.1)
Web DB Press Vol.82 の Go特集でやってたやつを python で書いてみる
hoge.py
import asyncio from urllib import request urls = [ 'http://example.com', 'http://example.net', 'http://example.org', ] async def display_status(url): r = request.urlopen(url) print('{}: {}'.format(url, r.code)) loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait([display_status(url) for url in urls])) loop.close()
実行
$ python3 hoge.py http://example.net: 200 http://example.com: 200 http://example.org: 200