4 Replies Latest reply on Aug 15, 2013 8:57 PM by DawMatt



      So I am calling LrHttp from an async function (because lrhttp has to be called from an async) function; however, I need the result of my LrHttp call before I move on to the next statements in my program.


      Is there a way to block things from proceeding until I get the result of Lrhttp? Essentially, I want to be able to call LrHttp from a non-async function (I don't want to start a background thread for LrHttp) so that the rest of my program waits until I get the result.


      EDIT: Maybe recursionguard can help in some way? I am not exactly sure how to use it, so if you can point me to an example (if it's pertinent) then that would be great.

        • 1. Re: Blocking
          areohbee Level 6

          You can always spin until a flag is set, e.g.


          local got_it = false

          LrTasks.startAsyncTask( function()

               got_it = true

          end )


               LrTasks.sleep( .1 )

          until got_it


          but note: the thing waiting for the other thing still has to be a task.


          Another possibility:

          Set the flag as a member of an observer property table instead, and have a property change observer do whatever needs to be done when you've "got_it".


          This way, the waiting thing does not have to be a (async) task.


          And yes, you may need to figure out some scheme for recursion guarding, depending on what is triggering your async tasks... - one step at a time though, eh?



          • 2. Re: Blocking
            DawMatt Level 3

            Rob Cole wrote:


            You can always spin until a flag is set, e.g.


            I thought the sleep also had to be within an LrTask, which would defeat the purpose in the code sample above. I'm sure I've tried this before.


            If I'm trying to write code that will use some blocking functions then I usually try to initiate my async tasks at as high a level in my code that makes sense.



            • If my code is going to be called from a menu item, then start the async task as the first step in the script that is called when the menu item is selected
            • If code is being called in response to a button press in the UI, then have the action function create the async task and use it to wrap the function that will do the real work
            • If I'm going to make a LrHttp call and process its results, then put that atomic unit of work in a single function and wrap the whole function call in the async task


            You don't need to wrap the LrHttp call in its own async task. It just needs to be inside an async task. But please be aware that if you make a call to create a dialog (e.g. LrDialogs) from within an async task, you will still need to explicitly create a new async task within any action functions or property observers that are triggered by the dialog. This has bitten me on occassions, and these types of errors can be hard to track down.



            • 3. Re: Blocking
              areohbee Level 6

              Matt Dawson wrote:


              I thought the sleep also had to be within an LrTask

              Yes, thus my comment "the thing waiting for the other thing still has to be a task" and my subsequent example of how to do it without such requirement.


              But I agree: generally if you wrap the outermost functionality in a task, it solves the problem (the thing waiting will be a task).



              • 4. Re: Blocking
                DawMatt Level 3

                OK. Sorry. My cold addled brain didn't interpret the "thing ... thing" comment in the way it was intended.  If the whole example needs to be wrapped in an async task anyway then you don't need an additiona child async task, or even the sleep if you remove the child async task.