V
V
Vitaly2021-02-15 03:15:44
Python
Vitaly, 2021-02-15 03:15:44

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

1 answer(s)
D
David on the headstock, 2021-02-15
@davGro

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 = {

The difference is noticeable, isn't it?
Use the correct number of spaces, at each level 4, before the loop in your case there should be 8 spaces, the more it affects the readability of the code.
The markup problem is not only in this function, you also have a dummy_task at an incomprehensible level. Don't use tabs , use 4 spaces instead of tabs. Otherwise, your interpreter will give you an error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question