More Related Content Similar to 20180320 python3 async_io (14) 20180320 python3 async_io2. Contents
1. Python 3
a. 3.6์ผ๋ก ์์
b. ์ ์ฉ ์์
c. ์ฃผ์ ๋ฌธ๋ฒ ๋ณํ
d. ์ด์๋คโฆ
e. ์ผ๋ง ๋จ์ง ์์๋ค
2. AsyncIO
a. ์๊ฐ
b. ์ผ๋ฐ ์์
c. ์ ์ฉ ์์
d. ์ฑ๋ฅ ์ฐจ์ด
e. ์
๋ฌด ์ ์ฉ ์ฌ๋ก
3. Summary
a. Python3
b. AsyncIO
4. 1. Python3 - a. 3.6์ผ๋ก ์์
โ async def์ await๋ ํ์ด์ฌ 3.5์์ ์ถ๊ฐ๋์์
โ ๋ฐ๋ผ์ ๊ทธ ์ดํ ๋ฒ์ ์์๋ ์ฌ์ฉํ ์ ์์
โ ํ์ด์ฌ 3.4์์๋ @asyncio.coroutine ๋ฐ์ฝ๋ ์ดํฐ๋ก ๋ค์ดํฐ๋ธ ์ฝ๋ฃจํด์ ์์ฑ
โ 3.6(stable version)์ผ๋ก ์์ํ๋๊ฒ์ ์ถ์ฒ
5. 1. Python3 - b. ์ ์ฉ ์์
1. Python 3.6 Download(https://www.python.org/)
2. Read Readme
a. configure
b. make
c. make test
d. sudo make install
3. install python package
a. using pip3.6
6. 1. Python3 - c. ์ฃผ์ ๋ฌธ๋ฒ ๋ณํ
Python 2 Python 3
int ๋๋ ๊ฒฐ๊ณผ float print( 1/2 ) # 0
print( type(1/2) ) # <type 'int'>
print( 1/2 ) # 0.5
print( type(1/2) ) # <class 'float'>
print๋ฌธ ๊ดํธ ํ์ print( 'hello' ) # hello
print 'hello' # hello
print( 'hello' ) # hello
print 'hello' # Error! invalid syntax
str๊ณผ unicode ํต์ผ print( type('hello') ) # <type 'str'>
print( type(u'hello') ) # <type 'unicode'>
print( type('hello') ) # <class 'str'>
print( type(u'hello') ) # <class 'str'>
long์ int๋ก ํต์ผ print( 2**30 ) # 1073741824
print( type(2**30) ) # <type 'int'>
print( 2**100 ) #
1267650600228229401496703205376
print( type(2**100) ) # <type 'long'>
print( 2**30 ) # 1073741824
print( type(2**30) ) # <class 'int'>
print( 2**100 ) #
1267650600228229401496703205376
print( type(2**100) ) # <class 'int'>
7. 1. Python3 - d. ์ด์๋ค...
1. int ๋๋๊ฒฐ๊ณผ float: ์ฌ์
2. print๋ฌธ ๊ดํธ ํ์: ์ฌ์
3. str๊ณผ unicode ํต์ผ: ๊ฒฝ์ฐ์ ๋ฐ๋ผ ์ด๋ ค์
4. long๊ณผ int ํต์ผ: ์ฌ์
5. dictionary์ iter~, view~ ์๋ฆฌ์ฆ ์ญ์ : ์ฌ์
6. ์ธ๋ถ package๋ค์ ํธํ์ฑ: ๊ฒฝ์ฐ์ ๋ฐ๋ผ ๊ท์ฐฎ์
7. except Exception, e: -> except Exception as e: ์ฌ์
8. 1. Python3 - e. ์ผ๋ง ๋จ์ง ์์๋ค
โ https://pythonclock.org/
11. 2. AsyncIO - b. ์ผ๋ฐ ์์
# Example 1: synchronous requests
import requests
num_requests = 20
responses = [
requests.get('http://example.org/')
for i in range(num_requests)
]
12. 2. AsyncIO - c. ์ ์ฉ ์์
# Example 2: asynchronous requests
import asyncio
import requests
async def main():
loop = asyncio.get_event_loop()
futures = [
loop.run_in_executor(
None,
requests.get,
'http://example.org/'
)
for i in range(20)
]
for response in await asyncio.gather(*futures):
pass
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
14. 2. AsyncIO - e. ์
๋ฌด ์ ์ฉ ์ฌ๋ก
loop = asyncio.get_event_loop() # ์ด๋ฒคํธ ๋ฃจํ๋ฅผ ์ป์
try_cnc_nodedata = loop.run_until_complete(cnc_nodedata.get_cnc_data(ACCOUNT_INFO, ACCOUNT_DOMAIN_MAP,
DOMAIN_INFO, start_datetime, end_datetime, req_date, headers, try_cnc_nodedata))
loop.close()
==========
def do_post_req():
return requests.post(url, data=json_data, headers=_headers, auth=(_id, _pwd), timeout=CONFIG["CNC_API_TIMEOUT"])
loop = asyncio.get_event_loop()
res = await loop.run_in_executor(None, do_post_req)
15. 2. AsyncIO - e. ์
๋ฌด ์ ์ฉ ์ฌ๋ก(cont.)
โ AsyncIO ์ ์ฉ ์
โ 270์ด ์ ๋ ์์.
โ domain์ ์ฆ๊ฐ์ ๋ฐ๋ผ ์์ ์๊ฐ ์ฆ๊ฐ
โ AsyncIO ์ ์ฉ ํ
โ 60์ด ์ ๋ ์์
โ domain์ ์ฆ๊ฐ์ ์ํฅ๋ฐ์ง ์์.
17. 3. Summary - a. Python3
โ Python์ v3๋ฅผ ์จ์ผ๋ง ํ๋ค.
โ v2 -> v3 ๋ ๋ช๋ช ๊ฒฝ์ฐ๋ฅผ ์ ์ธํ๊ณ ๋ ๊ทธ๋ฆฌ ์ด๋ ต์ง ์๋ค.
โ ์ง๊ธ ๋ฐ๋ก ๋์ด๊ฐ์ธ์~
18. 3. Summary - b. AsyncIO
โ IO๋น์ค์ด ๋๋ค๋ฉด ๋์ ๋๋ ์ฑ๋ฅ ๊ฐ์ !
โ ๊ทธ๋์ ๋ฌด์ํด์๋ Python์ Multi Thread๋ฅผ ์ ๊ทน ํ์ฉ.
โ async, await, event loop, native coroutine ๋ฑ์ ๊ฐ๋
์ study ํ์.