๐ŸํŒŒ์ด์ฌ 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์ดˆ๋กœ ์ค„์–ด ๋“ค์—ˆ์œผ๋ฉฐ
ํŽ˜์ด์ง€๋ฅผ ๋‹ซ์„ ๋•Œ ์ˆœ์„œ๊ฐ€ ์—ด๋•Œ์˜ ์ˆœ์„œ์™€ ๋‹ค๋ฅด๋‹ค.
์ฆ‰ ๋น„๋™๊ธฐ๋กœ ์ผ์ด ์ฒ˜๋ฆฌ ๋˜์—ˆ๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ