loop is not defined
2
7 asked
import asyncio
import timeit
from urllib.request import urlopen
from concurrent.futures import ThreadPoolExecutor
import threading
# 실행 시작 시간
start =timeit.default_timer()
# 서비스 방향이 비슷한 사이트로 실습권장(예 게시판 성 커뮤니티)
urls = ['https://daum.net', 'https://naver.com', 'http://mlbpark.donga.com', 'https://tistory.com']
async def fetch(url, executor):
# 실행
res = await loop.get_running_loop(executor, urlopen, url)
# 결과 반환
return res.read()[0:5]
async def main():
# 쓰레드 풀 생성
executor = ThreadPoolExecutor(max_workers=10)
# future 객체 모아서 gather 에서 실행
futures = [
asyncio.ensure_future(fetch(url, executor)) for url in urls
]
# 결과 취합
rst = await asyncio.gather(futures)
print('------------------------------')
print(('Result :', rst))
if __name__ == '__main__':
# 루프 초기화
asyncio.run(main())
# 수행 시간 계산
duration = timeit.default_timer() - start
# 총 실행 시간
print('Total Running Time :', duration)
여기에서 loop is not defined라고 나오네요 지금 파이선은 3.14버전을 쓰고있는데 저기 loop를 무엇으로 바꾸면 될지 모르겠어요
Answer 1
0
loop = asyncio.get_running_loop()
res = await loop.run_in_executor(executor, urlopen, url)이렇게 수정하니 해결이 됐습니다.
가상환경 설정이 안됩니다
0
5
0
__le__, __ge__ 관련 질문
0
31
0
atom 서비스 종료
0
37
1
atom 서비스 종료
0
39
1
진도미확인 (진도가 안 넘어감)
0
67
1
RuntimeError: There is no current event loop in thread 'MainThread'
0
109
1
chapter 07-01 실행 오류
0
90
1
Chapter05-04 : sum_func의 합이 이중 출력됩니다.
0
104
2
45. 병렬성 2 - Futures(5-1) wait가 의도된 대로 동작하지 않습니다.
0
124
2
멀티스레딩 - 44. 병렬성 1 - Futures(4-2)
0
100
2
chapter05_02 클로저 질문사항 !
0
79
1
LV1 설정
0
112
2
매직 메소드의 호출 방법 차이 문의
0
161
2
왜 numbers리스트를 만들때 str으로 숫자를 감싸나요?
0
121
2
스크랩핑 실습 중 Mac OS 인증서 문제
0
149
1
AsyncIO 멀티 스크랩핑 실습 예제 관련 질문
0
190
1
강의자료가 영상과 다릅니다
0
273
2
closure.cell_contents 초기화 하기 문의
0
154
2
map함수 사용 시, list변환 방법 문의
0
369
2
__mul__ 백터 * 숫지, 백터 * 백터 처리
0
164
1
del처리후 질문입니다.
0
180
1
car_list(car1,car2,car3) 인스턴스 tuple 타입 문의
0
248
1
coroutine에 대한 질문
0
261
1
제너레이터 이터레이터 질문이 있습니다.
0
265
1

