Answer the question
In order to leave comments, you need to log in
Why is the result of the post request (aiohttp, asyncio) not coming?
Confused about asyncio. Explain why the response from get_response() does not come? And why is the while True loop ignored in task_check_website(), i.e. get_response() is called once, there is no response, and only dummy_task() works normally, in which while True works as it should.
async def get_response(self, url, use_proxy=True, *args, **kwargs):
for _ in range(0, 60):
_headers = {
...
}
if use_proxy:
_proxy = self.get_proxy()
else:
_proxy = {}
try:
async with aiohttp.ClientSession() as sess:
async with sess.post(url, headers=_headers, proxy=_proxy, *args, **kwargs) as response:
r = await response.text()
if response.status > 399:
raise ScraperError(response.status)
await sleep(0.1)
return r
except (aiohttp.ClientError, ScraperError) as err:
await sleep(0.5)
continue
return None
async def dummy_task(self):
j = 0
while True:
print("%ds" % j)
await sleep(1)
j += 1
async def task_check_website(self):
while True:
_data = {
"next": "Next",
"id": str(self.data["task"]["type_id"]),
} # deb
r = await self.get_response(self.data["curr_url"], data=_data)
await sleep(1)
async def run(self):
self.data["worker"]["tasks"].append(asyncio.create_task(self.task_check_website()))
self.data["worker"]["tasks"].append(asyncio.create_task(self.dummy_task()))
await asyncio.gather(*self.data["worker"]["tasks"], return_exceptions=True)
Answer the question
In order to leave comments, you need to log in
Your loop is probably being ignored because your markup is bad.
# ваш код
async def task_check_website(self):
while True:
_data = {
# правильная разметка
async def task_check_website(self):
while True:
_data = {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question