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.


    Best way to throttle network requests

    Pythonista
    3
    3
    1868
    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.
    • mikael
      mikael last edited by

      What would be your recommended way of handling the following use case, specifically in Pythonista UI module context:

      User is busily typing in a TextView (or a similar UI element). I want the input to be stored on a web service (via HTTP POST).

      Saving to the web service is generally fast but subject to all the vagaries of Internet, so I would not like to send an update for every character typed, nor to wait for the response. And since I am sending the whole text typed every time, it does not matter if an occasional update goes awry - but, if the "last" update fails, it needs to be re-sent.

      Any suggestions? This seems such a common scenario that I am hoping there's an "established" solution for it.

      Thanks!

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

        A few things come to mind. In textview_did_change, you could call ui.delay to a function which immediately calls ui.cancel_delays, so that data is only sent when the user stops typing for a bit.

        The actual send should probably be in a ui.in_background, or thread.

        you might also keep the connection alive using requests.Session.

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

          Another approach would be to use textview_did_change as @JonB recommends and then send your data using Python3's asyncio module. Send and forget. You will send more packets to your server than JonB's method but each packet's progress should not block your UI.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors