1
이 예제 중 하나를 실행하려했는데, page입니다. 당신이이 프로그램을 실행하면 어떤 이유로 "hi"를 무작위로 인쇄하고 멈추게된다. 파이썬 3.6.0을 사용하고 있는데, 문제가 무엇인지 알아내는 데 도움이 될 수 있습니까?어떤 이유에서든 Python asyncio가 멈추었습니다.
import asyncio
import aiohttp
import bs4
@asyncio.coroutine
def print_page(url):
response = yield from aiohttp.request('GET', url)
body = yield from response.text()
print(body)
@asyncio.coroutine
def get(*args, **kwargs):
response = yield from aiohttp.request('GET', *args, **kwargs)
return (yield from response.text())
def first_magnet(page):
return "hi"
@asyncio.coroutine
def print_magnet(query):
url = 'http://www.aclweb.org/anthology/J/J{}/'.format(query)
page = yield from get(url, compress=True)
magnet = first_magnet(page)
print('{}: {}'.format(query, magnet))
distros = range(80,90)
loop = asyncio.get_event_loop()
f = asyncio.wait([print_magnet(d) for d in distros])
loop.run_until_complete(f)
'return (yield from response.text())'이상하게 들린다. –
코드가 잘 돌아가고,'8x : hi'를 10 번 인쇄하면 프로그램이 제대로 돌아 간다. 또한 파이썬> = 3.5를 사용하는 경우 [async/await] (http://asyncio.readthedocs.io/en/latest/hello_world.html#simple-coroutine) 구문을 사용하는 것이 좋습니다. – Vincent