-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappendix3.py
More file actions
87 lines (69 loc) · 3.71 KB
/
appendix3.py
File metadata and controls
87 lines (69 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 메타클래스 활용하기
class Singleton(type): # type을 상속받음
__instances = {} # 클래스의 인스턴스를 저장할 속성
def __call__(cls, *args, **kwargs): # 클래스로 인스턴스를 만들 때 호출되는 메서드
if cls not in cls.__instances: # 클래스로 인스턴스를 생성하지 않았는지 확인
cls.__instances[cls] = super().__call__(*args, **kwargs)
# 생성하지 않았으면 인스턴스를 생성하여 속성에 저장
return cls.__instances[cls] # 클래스로 인스턴스를 생성했으면 인스턴스 반환
class Hello(metaclass=Singleton): # 메타클래스로 Singleton을 지정
pass
a = Hello() # 클래스 Hello로 인스턴스 a 생성
b = Hello() # 클래스 Hello로 인스턴스 b 생성
print(a is b) # True: 인스턴스 a와 b는 같음
# 네이티브 코루틴 만들기
import asyncio
async def hello(): # async def로 네이티브 코루틴을 만듦
print('Hello, world!')
loop = asyncio.get_event_loop() # 이벤트 루프를 얻음
loop.run_until_complete(hello()) # hello가 끝날 때까지 기다림
loop.close() # 이벤트 루프를 닫음
# await로 네이트브 코루틴 실행하기
import asyncio
async def add(a, b):
print('add: {0} + {1}'.format(a, b))
await asyncio.sleep(1.0) # 1초 대기. asyncio.sleep도 네이티브 코루틴
return a + b # 두 수를 더한 결과 반환
async def print_add(a, b):
result = await add(a, b) # await로 다른 네이티브 코루틴 실행하고 반환값을 변수에 저장
print('print_add: {0} + {1} = {2}'.format(a, b, result))
loop = asyncio.get_event_loop() # 이벤트 루프를 얻음
loop.run_until_complete(print_add(1, 2)) # print_add가 끝날 때까지 이벤트 루프를 실행
loop.close() # 이벤트 루프를 닫음
# 비동기로 웹 페이지 가져오기
from time import time
from urllib.request import Request, urlopen
urls = ['https://www.google.co.kr/search?q=' + i
for i in ['apple', 'pear', 'grape', 'pineapple', 'orange', 'strawberry']]
begin = time()
result = []
for url in urls:
request = Request(url, headers={'User-Agent': 'Mozilla/5.0'}) # UA가 없으면 403 에러 발생
response = urlopen(request)
page = response.read()
result.append(len(page))
print(result)
end = time()
print('실행 시간: {0:.3f}초'.format(end - begin))
# asyncio 사용해서 웹 페이지를 비동기로 가져오기
from time import time
from urllib.request import Request, urlopen
import asyncio
urls = ['https://www.google.co.kr/search?q=' + i
for i in ['apple', 'pear', 'grape', 'pineapple', 'orange', 'strawberry']]
async def fetch(url):
request = Request(url, headers={'User-Agent': 'Mozilla/5.0'}) # UA가 없으면 403 에러 발생
response = await loop.run_in_executor(None, urlopen, request) # run_in_executor 사용
page = await loop.run_in_executor(None, response.read) # run in executor 사용
return len(page)
async def main():
futures = [asyncio.ensure_future(fetch(url)) for url in urls]
# 태스크(퓨처) 객체를 리스트로 만듦
result = await asyncio.gather(*futures) # 결과를 한꺼번에 가져옴
print(result)
begin = time()
loop = asyncio.get_event_loop() # 이벤트 루프를 얻음
loop.run_until_complete(main()) # main이 끝날 때까지 기다림
loop.close() # 이벤트 루프를 닫음
end = time()
print('실행 시간: {0:.3f}초'.format(end - begin))