Handling async responses immediately












2















I need to parse repeatedly one link content. synchronous way gives me 2-3 responses per second, i need faster (yes, i know, that too fast is bad too)



I found some async examples, but all of them show how to handle result after all links are parsed, whereas i need to parse it immediately after receiving, something like this, but this code doesn't give any speed improvement:



import aiohttp
import asyncio
import time
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()

async def main():
while True:
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'https://example.com')
print(time.time())
#do_something_with_html(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())









share|improve this question

























  • 2-3 is "too slow" but "too fast is bad also", what are you looking for?

    – Chad
    Jan 1 at 23:41











  • maybe 10-20 req per sec

    – vreter
    Jan 2 at 0:55






  • 1





    Similar question answered here that still makes use of asyncio: stackoverflow.com/questions/54156503/…

    – dtanabe
    Jan 13 at 16:16
















2















I need to parse repeatedly one link content. synchronous way gives me 2-3 responses per second, i need faster (yes, i know, that too fast is bad too)



I found some async examples, but all of them show how to handle result after all links are parsed, whereas i need to parse it immediately after receiving, something like this, but this code doesn't give any speed improvement:



import aiohttp
import asyncio
import time
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()

async def main():
while True:
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'https://example.com')
print(time.time())
#do_something_with_html(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())









share|improve this question

























  • 2-3 is "too slow" but "too fast is bad also", what are you looking for?

    – Chad
    Jan 1 at 23:41











  • maybe 10-20 req per sec

    – vreter
    Jan 2 at 0:55






  • 1





    Similar question answered here that still makes use of asyncio: stackoverflow.com/questions/54156503/…

    – dtanabe
    Jan 13 at 16:16














2












2








2








I need to parse repeatedly one link content. synchronous way gives me 2-3 responses per second, i need faster (yes, i know, that too fast is bad too)



I found some async examples, but all of them show how to handle result after all links are parsed, whereas i need to parse it immediately after receiving, something like this, but this code doesn't give any speed improvement:



import aiohttp
import asyncio
import time
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()

async def main():
while True:
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'https://example.com')
print(time.time())
#do_something_with_html(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())









share|improve this question
















I need to parse repeatedly one link content. synchronous way gives me 2-3 responses per second, i need faster (yes, i know, that too fast is bad too)



I found some async examples, but all of them show how to handle result after all links are parsed, whereas i need to parse it immediately after receiving, something like this, but this code doesn't give any speed improvement:



import aiohttp
import asyncio
import time
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()

async def main():
while True:
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'https://example.com')
print(time.time())
#do_something_with_html(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())






python python-3.x asynchronous python-asyncio aiohttp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 14 at 0:42









Messa

16.4k23253




16.4k23253










asked Jan 1 at 23:15









vretervreter

163




163













  • 2-3 is "too slow" but "too fast is bad also", what are you looking for?

    – Chad
    Jan 1 at 23:41











  • maybe 10-20 req per sec

    – vreter
    Jan 2 at 0:55






  • 1





    Similar question answered here that still makes use of asyncio: stackoverflow.com/questions/54156503/…

    – dtanabe
    Jan 13 at 16:16



















  • 2-3 is "too slow" but "too fast is bad also", what are you looking for?

    – Chad
    Jan 1 at 23:41











  • maybe 10-20 req per sec

    – vreter
    Jan 2 at 0:55






  • 1





    Similar question answered here that still makes use of asyncio: stackoverflow.com/questions/54156503/…

    – dtanabe
    Jan 13 at 16:16

















2-3 is "too slow" but "too fast is bad also", what are you looking for?

– Chad
Jan 1 at 23:41





2-3 is "too slow" but "too fast is bad also", what are you looking for?

– Chad
Jan 1 at 23:41













maybe 10-20 req per sec

– vreter
Jan 2 at 0:55





maybe 10-20 req per sec

– vreter
Jan 2 at 0:55




1




1





Similar question answered here that still makes use of asyncio: stackoverflow.com/questions/54156503/…

– dtanabe
Jan 13 at 16:16





Similar question answered here that still makes use of asyncio: stackoverflow.com/questions/54156503/…

– dtanabe
Jan 13 at 16:16












2 Answers
2






active

oldest

votes


















0














I gave up using async, threading solved my problem, thanks to this answer
https://stackoverflow.com/a/23102874/5678457



from threading import Thread
import requests
import time
class myClassA(Thread):
def __init__(self):
Thread.__init__(self)
self.daemon = True
self.start()
def run(self):
while True:
r = requests.get('https://ex.com')
print(r.status_code, time.time())
for i in range(5):
myClassA()





share|improve this answer































    0















    but this code doesn't give any speed improvement




    asyncio (and async/concurrency in general) gives speed improvement for I/O things that interleave each other.



    When everything you do is await something and you never create any parallel tasks (using asyncio.create_task(), asyncio.ensure_future() etc.) then you are basically doing the classic synchronous programming :)



    So, how to make the requests faster:



    import aiohttp
    import asyncio
    import time

    async def fetch(session, url):
    async with session.get(url) as response:
    return await response.text()

    async def check_link(session):
    html = await fetch(session, 'https://example.com')
    print(time.time())
    #do_something_with_html(html)

    async def main():
    async with aiohttp.ClientSession() as session:
    while True:
    asyncio.create_task(check_link(session))
    await asyncio.sleep(0.05)

    asyncio.run(main())


    Notice: the async with aiohttp.Cliensession() as session: must be above (outside) while True: for this to work. Actually, having a single ClientSession() for all your requests is a good practice anyway.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999676%2fhandling-async-responses-immediately%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I gave up using async, threading solved my problem, thanks to this answer
      https://stackoverflow.com/a/23102874/5678457



      from threading import Thread
      import requests
      import time
      class myClassA(Thread):
      def __init__(self):
      Thread.__init__(self)
      self.daemon = True
      self.start()
      def run(self):
      while True:
      r = requests.get('https://ex.com')
      print(r.status_code, time.time())
      for i in range(5):
      myClassA()





      share|improve this answer




























        0














        I gave up using async, threading solved my problem, thanks to this answer
        https://stackoverflow.com/a/23102874/5678457



        from threading import Thread
        import requests
        import time
        class myClassA(Thread):
        def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()
        def run(self):
        while True:
        r = requests.get('https://ex.com')
        print(r.status_code, time.time())
        for i in range(5):
        myClassA()





        share|improve this answer


























          0












          0








          0







          I gave up using async, threading solved my problem, thanks to this answer
          https://stackoverflow.com/a/23102874/5678457



          from threading import Thread
          import requests
          import time
          class myClassA(Thread):
          def __init__(self):
          Thread.__init__(self)
          self.daemon = True
          self.start()
          def run(self):
          while True:
          r = requests.get('https://ex.com')
          print(r.status_code, time.time())
          for i in range(5):
          myClassA()





          share|improve this answer













          I gave up using async, threading solved my problem, thanks to this answer
          https://stackoverflow.com/a/23102874/5678457



          from threading import Thread
          import requests
          import time
          class myClassA(Thread):
          def __init__(self):
          Thread.__init__(self)
          self.daemon = True
          self.start()
          def run(self):
          while True:
          r = requests.get('https://ex.com')
          print(r.status_code, time.time())
          for i in range(5):
          myClassA()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 12:26









          vretervreter

          163




          163

























              0















              but this code doesn't give any speed improvement




              asyncio (and async/concurrency in general) gives speed improvement for I/O things that interleave each other.



              When everything you do is await something and you never create any parallel tasks (using asyncio.create_task(), asyncio.ensure_future() etc.) then you are basically doing the classic synchronous programming :)



              So, how to make the requests faster:



              import aiohttp
              import asyncio
              import time

              async def fetch(session, url):
              async with session.get(url) as response:
              return await response.text()

              async def check_link(session):
              html = await fetch(session, 'https://example.com')
              print(time.time())
              #do_something_with_html(html)

              async def main():
              async with aiohttp.ClientSession() as session:
              while True:
              asyncio.create_task(check_link(session))
              await asyncio.sleep(0.05)

              asyncio.run(main())


              Notice: the async with aiohttp.Cliensession() as session: must be above (outside) while True: for this to work. Actually, having a single ClientSession() for all your requests is a good practice anyway.






              share|improve this answer






























                0















                but this code doesn't give any speed improvement




                asyncio (and async/concurrency in general) gives speed improvement for I/O things that interleave each other.



                When everything you do is await something and you never create any parallel tasks (using asyncio.create_task(), asyncio.ensure_future() etc.) then you are basically doing the classic synchronous programming :)



                So, how to make the requests faster:



                import aiohttp
                import asyncio
                import time

                async def fetch(session, url):
                async with session.get(url) as response:
                return await response.text()

                async def check_link(session):
                html = await fetch(session, 'https://example.com')
                print(time.time())
                #do_something_with_html(html)

                async def main():
                async with aiohttp.ClientSession() as session:
                while True:
                asyncio.create_task(check_link(session))
                await asyncio.sleep(0.05)

                asyncio.run(main())


                Notice: the async with aiohttp.Cliensession() as session: must be above (outside) while True: for this to work. Actually, having a single ClientSession() for all your requests is a good practice anyway.






                share|improve this answer




























                  0












                  0








                  0








                  but this code doesn't give any speed improvement




                  asyncio (and async/concurrency in general) gives speed improvement for I/O things that interleave each other.



                  When everything you do is await something and you never create any parallel tasks (using asyncio.create_task(), asyncio.ensure_future() etc.) then you are basically doing the classic synchronous programming :)



                  So, how to make the requests faster:



                  import aiohttp
                  import asyncio
                  import time

                  async def fetch(session, url):
                  async with session.get(url) as response:
                  return await response.text()

                  async def check_link(session):
                  html = await fetch(session, 'https://example.com')
                  print(time.time())
                  #do_something_with_html(html)

                  async def main():
                  async with aiohttp.ClientSession() as session:
                  while True:
                  asyncio.create_task(check_link(session))
                  await asyncio.sleep(0.05)

                  asyncio.run(main())


                  Notice: the async with aiohttp.Cliensession() as session: must be above (outside) while True: for this to work. Actually, having a single ClientSession() for all your requests is a good practice anyway.






                  share|improve this answer
















                  but this code doesn't give any speed improvement




                  asyncio (and async/concurrency in general) gives speed improvement for I/O things that interleave each other.



                  When everything you do is await something and you never create any parallel tasks (using asyncio.create_task(), asyncio.ensure_future() etc.) then you are basically doing the classic synchronous programming :)



                  So, how to make the requests faster:



                  import aiohttp
                  import asyncio
                  import time

                  async def fetch(session, url):
                  async with session.get(url) as response:
                  return await response.text()

                  async def check_link(session):
                  html = await fetch(session, 'https://example.com')
                  print(time.time())
                  #do_something_with_html(html)

                  async def main():
                  async with aiohttp.ClientSession() as session:
                  while True:
                  asyncio.create_task(check_link(session))
                  await asyncio.sleep(0.05)

                  asyncio.run(main())


                  Notice: the async with aiohttp.Cliensession() as session: must be above (outside) while True: for this to work. Actually, having a single ClientSession() for all your requests is a good practice anyway.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 14 at 0:22

























                  answered Jan 13 at 7:04









                  MessaMessa

                  16.4k23253




                  16.4k23253






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999676%2fhandling-async-responses-immediately%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      'app-layout' is not a known element: how to share Component with different Modules

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      SQL update select statement