๐ํ์ด์ฌ asyncio
ํ์ด์ฌ asyncio๋ฅผ ํ์ฉํ์ฌ ์นํ์ด์ง๋ฅผ ์ด๊ณ ๋ซ๋ ํ๋ก๊ทธ๋จ์ ์๊ฐ์ ๋จ์ถ ์์ผ๋ณด์.
์์ฐจ ์คํ
๋ค์, ๊ตฌ๊ธ, ๋ค์ด๋ฒ, ๋ฒจ๋ก๊ทธ, ๊นํ๋ธ, ๋ฌด์ ์ฌ ํํ์ด์ง๋ฅผ ์์ฐจ์ ์ผ๋ก ์ฌ๋ ๋ฐฉ์์ผ๋ก ์์ฑํด๋ณด์.
import timeit
from urllib.request import urlopen
urls =['https://daum.net', 'https://google.com', 'https://naver.com', 'https://velog.io/', 'https://github.com/', 'https://www.musinsa.com/']
start = timeit.default_timer()
# ์์ฐจ ์คํ๋ถ
for url in urls:
print('Start', url)
urlopen(url)
print('Done', url)
# ์๋ฃ์๊ฐ - ์์์๊ฐ
duration = timeit.default_timer() - start
# ์ด ์คํ ์๊ฐ
print('Total Time : ', duration)
# ๊ฒฐ๊ณผ
Start https://daum.net
Done https://daum.net
Start https://google.com
Done https://google.com
Start https://naver.com
Done https://naver.com
Start https://velog.io/
Done https://velog.io/
Start https://github.com/
Done https://github.com/
Start https://www.musinsa.com/
Done https://www.musinsa.com/
Total Time : 1.797806497
๋ฐ๋ณต๋ฌธ์ผ๋ก urlopen ํจ์๋ฅผ ์ฌ์ฉํด์ ๊ฐ ํํ์ด์ง๋ฅผ ์ฌ๋ ํ๋ก๊ทธ๋จ์ด๋ค.
๊ฐํํ์ด์ง๋ฅผ ์ด๊ณ ์์
์ ๋ง์น๊ณ ๋ค์ ํ์ด์ง๋ฅผ ์ฐ๋ค.
์ด 1.8์ด ์ ๋๊ฐ ๊ฑธ๋ ธ๋ค.
asyncio์คํ
์์ ํ๋ก๊ทธ๋จ์์ ์นํ์ด์ง๋ฅผ ์ฌ๋๋์ ๋ค๋ฅธ ์นํ์ด์ง๋ ์ฌ๋ ๋น๋๊ธฐ ๋ฐฉ์์ผ๋ก ๋ฐ๊ฟ๋ณด์.
import timeit
from urllib.request import urlopen
โญ๏ธimport asyncio
urls =['https://daum.net', 'https://google.com', 'https://naver.com', 'https://velog.io/', 'https://github.com/', 'https://www.musinsa.com/']
start = timeit.default_timer()
if __name__ == '__main__':
# ๋ฃจํ ์์ฑ
loop = asyncio.get_event_loop()
# ๋ฃจํ ๋๊ธฐ
loop.run_until_complete(main())
# ๋ฃจํ ์ข
๋ฃ
loop.close()
# ์๋ฃ์๊ฐ - ์์์๊ฐ
duration = timeit.default_timer() - start
# ์ด ์คํ ์๊ฐ
print('Total Time : ', duration)
loop = asyncio.get_event_loop()
์ด๋ฒคํธ ๋ฃจํ๋ฅผ ์์ฑํ๊ณ
loop.run_until_complete(main())
์ด๋ฒคํธ ๋ฃจํ๊ฐ ๋๋ ๋ ๊น์ง ๋๊ธฐํ๊ณ ๋ซ์์ค๋ค.
mainํจ์๋ฅผ ๊ตฌํํด๋ณด์.
import timeit
from urllib.request import urlopen
import asyncio
urls =['https://daum.net', 'https://google.com', 'https://naver.com', 'https://velog.io/', 'https://github.com/', 'https://www.musinsa.com/']
start = timeit.default_timer()
โญ๏ธasync def main():
# asyncio.ensure_future :
futures = [
asyncio.ensure_future(fetch(url)) for url in urls
]
# ๊ฒฐ๊ณผ ์ทจํฉ
rst = await asyncio.gather(*futures)
print()
print('Result : ', rst)
if __name__ == '__main__':
# ๋ฃจํ ์์ฑ
loop = asyncio.get_event_loop()
# ๋ฃจํ ๋๊ธฐ
loop.run_until_complete(main())
# ๋ฃจํ ์ข
๋ฃ
loop.close()
# ์๋ฃ์๊ฐ - ์์์๊ฐ
duration = timeit.default_timer() - start
# ์ด ์คํ ์๊ฐ
print('Total Time : ', duration)
futures = [ asyncio.ensure_future(fetch(url)) for url in urls ]
ํ์คํฌ ๊ฐ์ฒด(ํจ์ฒ๊ฐ์ฒด)๋ฅผ ๋ง๋ค์ด์ ๋ฆฌ์คํธ์ ๋ด์์ค๋ค.
rst = await asyncio.gather(*futures)
ํ
์คํฌ ๊ฐ์ฒด๊ฐ ๋ค ๋๋ ๋ ๊น์ง ๊ธฐ๋ค๋ ธ๋ค๊ฐ ๊ฒฐ๊ณผ ๊ฐ์ ํ๋ฒ์ ๋ด๋๋ค.
url์ ์ฌ๋ fetchํจ์๋ฅผ ๊ตฌํ ํด๋ณด์.
```python
import timeit
from urllib.request import urlopen
import asyncio
urls =['https://daum.net', 'https://google.com', 'https://naver.com', 'https://velog.io/', 'https://github.com/', 'https://www.musinsa.com/']
start = timeit.default_timer()
โญ๏ธasync def fetch(url):
print('Start', url)
# ์คํ
res = await loop.run_in_executor(None, urlopen, url)
print('Done', url)
# ๋ฐํ
return res.read()[0:5]
async def main():
# asyncio.ensure_future :
futures = [
asyncio.ensure_future(fetch(url)) for url in urls
]
# ๊ฒฐ๊ณผ ์ทจํฉ
rst = await asyncio.gather(*futures)
print()
print('Result : ', rst)
if __name__ == '__main__':
# ๋ฃจํ ์์ฑ
loop = asyncio.get_event_loop()
# ๋ฃจํ ๋๊ธฐ
loop.run_until_complete(main())
# ๋ฃจํ ์ข
๋ฃ
loop.close()
# ์๋ฃ์๊ฐ - ์์์๊ฐ
duration = timeit.default_timer() - start
# ์ด ์คํ ์๊ฐ
print('Total Time : ', duration)
res = await loop.run_in_executor(None, urlopen, url)
๋ค์ดํฐ๋ธ ์ฝ๋ฃจํด ์์์ ๋ธ๋กํน I/O ํจ์๋ฅผ ์คํํ๋ ค๋ฉด ์ด๋ฒคํธ ๋ฃจํ์ run_in_executor ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ค๋ฅธ ์ค๋ ๋์์ ๋ณ๋ ฌ๋ก ์คํ์์ผ์ผ ํ๋ค.
์ด์ ์คํ์ ํด๋ณด์.
Start https://daum.net
Start https://google.com
Start https://naver.com
Start https://velog.io/
Start https://github.com/
Start https://www.musinsa.com/
Done https://github.com/
Done https://naver.com
Done https://daum.net
Done https://www.musinsa.com/
Done https://velog.io/
Done https://google.com
Result : [b'<!DOC', b'<!doc', b'\n<!do', b'<!doc', b'\n\n\n\n\n', b'<!DOC']
Total Time : 0.528519932
์คํ ์๊ฐ์ด 0.5์ด๋ก ์ค์ด ๋ค์์ผ๋ฉฐ
ํ์ด์ง๋ฅผ ๋ซ์ ๋ ์์๊ฐ ์ด๋์ ์์์ ๋ค๋ฅด๋ค.
์ฆ ๋น๋๊ธฐ๋ก ์ผ์ด ์ฒ๋ฆฌ ๋์๋ค.
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(๐ํ์ด์ฌ asyncio), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@sdk1926/ํ์ด์ฌ-asyncio์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค