omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Asynchronous network requests

    Pythonista
    2
    4
    2539
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • userista
      userista last edited by

      I'm not sure how to do this in Python, and specifically in Pythonista - I want to make 4 asynchronous (and concurrent) requests and parse the responses of each of them ( assign each to a specific variable). Is there a concept of a callback in Python?

      Pseudo code:
      (

      async_requests.get('http://google.com', google_callback)
      async_requests.get('http://bing.com', bing_callback)
      async_requests.get('http://yahoo.com', yahoo_callback)
      async_requests.get('http://facebook.com', facebook_callback)
      
      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        See: threaded_network_requests.py. It does not feature the callbacks but they could be added. There is a preformance difference between the serial and parallel runs.

        1 Reply Last reply Reply Quote 0
        • userista
          userista last edited by

          Cool! Glanced at it and it looks like what I needed - I'll dig in more a bit later. Thanks!

          1 Reply Last reply Reply Quote 0
          • userista
            userista last edited by

            Yup! - this is exactly what I was looking for - concurrent network requests. I implemented it without callbacks, rather did it procedurally - after the request returned I passed the data to a different (new) method in the Worker class.

                def run(self):
                    data = requests.get(self.url).text
                    results[self.key] = self.parse(data)
            
                def parse(self, responseText):
                    #stuff here
                    return stuff
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors