F# run blocking call on another thread, use in async workflow











up vote
2
down vote

favorite












I have a blocking call blockingFoo() that I would like to use in an async context. I would like to run it on another thread, so as to not block the async.



Here is my solution:



let asyncFoo = 
async {
blockingFoo() |> ignore
}
|> Async.StartAsTask
|> Async.AwaitTask



  • Is this the correct way to do this?


  • Will this work as expected?











share|improve this question


























    up vote
    2
    down vote

    favorite












    I have a blocking call blockingFoo() that I would like to use in an async context. I would like to run it on another thread, so as to not block the async.



    Here is my solution:



    let asyncFoo = 
    async {
    blockingFoo() |> ignore
    }
    |> Async.StartAsTask
    |> Async.AwaitTask



    • Is this the correct way to do this?


    • Will this work as expected?











    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a blocking call blockingFoo() that I would like to use in an async context. I would like to run it on another thread, so as to not block the async.



      Here is my solution:



      let asyncFoo = 
      async {
      blockingFoo() |> ignore
      }
      |> Async.StartAsTask
      |> Async.AwaitTask



      • Is this the correct way to do this?


      • Will this work as expected?











      share|improve this question













      I have a blocking call blockingFoo() that I would like to use in an async context. I would like to run it on another thread, so as to not block the async.



      Here is my solution:



      let asyncFoo = 
      async {
      blockingFoo() |> ignore
      }
      |> Async.StartAsTask
      |> Async.AwaitTask



      • Is this the correct way to do this?


      • Will this work as expected?








      asynchronous f#






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 16:10









      sdgfsdh

      7,79673789




      7,79673789
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          I think you're a bit lost. Async.StartAsTask followed by Async.AwaitTask effectively cancel each other, with the side-effect that the Task created in the process actually triggers evaluation of the async block containing blockingFoo on the thread pool. So it works, but in a way that betrays expectations.



          If you want to trigger evaluation of asyncFoo from within another async block, a more natural way to do it would be to use Async.Start if you don't want to await its completion, or Async.StartChild if you do.



          let asyncFoo = 
          async {
          blockingFoo() |> ignore
          }

          async {
          // "fire and forget"
          asyncFoo |> Async.Start

          // trigger the computation
          let! comp = Async.StartChild asyncFoo

          // do other work here while comp is executing

          // await the results of comp
          do! comp
          }





          share|improve this answer





















          • How should I rewrite asyncFoo such that I don't have to remember to do Async.StartChild on each call?
            – sdgfsdh
            Nov 23 at 11:31










          • Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing the Async.StartChild on each call, because it makes it easier to tell what is happening. But if calling asyncFoo will be a common operation and the fact it kicks off a computation in the background will be well understood by the users, then you might want to "hide" it in order to have cleaner code.
            – scrwtp
            Nov 23 at 14:00











          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',
          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%2f53434764%2ff-run-blocking-call-on-another-thread-use-in-async-workflow%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote













          I think you're a bit lost. Async.StartAsTask followed by Async.AwaitTask effectively cancel each other, with the side-effect that the Task created in the process actually triggers evaluation of the async block containing blockingFoo on the thread pool. So it works, but in a way that betrays expectations.



          If you want to trigger evaluation of asyncFoo from within another async block, a more natural way to do it would be to use Async.Start if you don't want to await its completion, or Async.StartChild if you do.



          let asyncFoo = 
          async {
          blockingFoo() |> ignore
          }

          async {
          // "fire and forget"
          asyncFoo |> Async.Start

          // trigger the computation
          let! comp = Async.StartChild asyncFoo

          // do other work here while comp is executing

          // await the results of comp
          do! comp
          }





          share|improve this answer





















          • How should I rewrite asyncFoo such that I don't have to remember to do Async.StartChild on each call?
            – sdgfsdh
            Nov 23 at 11:31










          • Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing the Async.StartChild on each call, because it makes it easier to tell what is happening. But if calling asyncFoo will be a common operation and the fact it kicks off a computation in the background will be well understood by the users, then you might want to "hide" it in order to have cleaner code.
            – scrwtp
            Nov 23 at 14:00















          up vote
          4
          down vote













          I think you're a bit lost. Async.StartAsTask followed by Async.AwaitTask effectively cancel each other, with the side-effect that the Task created in the process actually triggers evaluation of the async block containing blockingFoo on the thread pool. So it works, but in a way that betrays expectations.



          If you want to trigger evaluation of asyncFoo from within another async block, a more natural way to do it would be to use Async.Start if you don't want to await its completion, or Async.StartChild if you do.



          let asyncFoo = 
          async {
          blockingFoo() |> ignore
          }

          async {
          // "fire and forget"
          asyncFoo |> Async.Start

          // trigger the computation
          let! comp = Async.StartChild asyncFoo

          // do other work here while comp is executing

          // await the results of comp
          do! comp
          }





          share|improve this answer





















          • How should I rewrite asyncFoo such that I don't have to remember to do Async.StartChild on each call?
            – sdgfsdh
            Nov 23 at 11:31










          • Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing the Async.StartChild on each call, because it makes it easier to tell what is happening. But if calling asyncFoo will be a common operation and the fact it kicks off a computation in the background will be well understood by the users, then you might want to "hide" it in order to have cleaner code.
            – scrwtp
            Nov 23 at 14:00













          up vote
          4
          down vote










          up vote
          4
          down vote









          I think you're a bit lost. Async.StartAsTask followed by Async.AwaitTask effectively cancel each other, with the side-effect that the Task created in the process actually triggers evaluation of the async block containing blockingFoo on the thread pool. So it works, but in a way that betrays expectations.



          If you want to trigger evaluation of asyncFoo from within another async block, a more natural way to do it would be to use Async.Start if you don't want to await its completion, or Async.StartChild if you do.



          let asyncFoo = 
          async {
          blockingFoo() |> ignore
          }

          async {
          // "fire and forget"
          asyncFoo |> Async.Start

          // trigger the computation
          let! comp = Async.StartChild asyncFoo

          // do other work here while comp is executing

          // await the results of comp
          do! comp
          }





          share|improve this answer












          I think you're a bit lost. Async.StartAsTask followed by Async.AwaitTask effectively cancel each other, with the side-effect that the Task created in the process actually triggers evaluation of the async block containing blockingFoo on the thread pool. So it works, but in a way that betrays expectations.



          If you want to trigger evaluation of asyncFoo from within another async block, a more natural way to do it would be to use Async.Start if you don't want to await its completion, or Async.StartChild if you do.



          let asyncFoo = 
          async {
          blockingFoo() |> ignore
          }

          async {
          // "fire and forget"
          asyncFoo |> Async.Start

          // trigger the computation
          let! comp = Async.StartChild asyncFoo

          // do other work here while comp is executing

          // await the results of comp
          do! comp
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 17:01









          scrwtp

          11.1k11626




          11.1k11626












          • How should I rewrite asyncFoo such that I don't have to remember to do Async.StartChild on each call?
            – sdgfsdh
            Nov 23 at 11:31










          • Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing the Async.StartChild on each call, because it makes it easier to tell what is happening. But if calling asyncFoo will be a common operation and the fact it kicks off a computation in the background will be well understood by the users, then you might want to "hide" it in order to have cleaner code.
            – scrwtp
            Nov 23 at 14:00


















          • How should I rewrite asyncFoo such that I don't have to remember to do Async.StartChild on each call?
            – sdgfsdh
            Nov 23 at 11:31










          • Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing the Async.StartChild on each call, because it makes it easier to tell what is happening. But if calling asyncFoo will be a common operation and the fact it kicks off a computation in the background will be well understood by the users, then you might want to "hide" it in order to have cleaner code.
            – scrwtp
            Nov 23 at 14:00
















          How should I rewrite asyncFoo such that I don't have to remember to do Async.StartChild on each call?
          – sdgfsdh
          Nov 23 at 11:31




          How should I rewrite asyncFoo such that I don't have to remember to do Async.StartChild on each call?
          – sdgfsdh
          Nov 23 at 11:31












          Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing the Async.StartChild on each call, because it makes it easier to tell what is happening. But if calling asyncFoo will be a common operation and the fact it kicks off a computation in the background will be well understood by the users, then you might want to "hide" it in order to have cleaner code.
          – scrwtp
          Nov 23 at 14:00




          Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing the Async.StartChild on each call, because it makes it easier to tell what is happening. But if calling asyncFoo will be a common operation and the fact it kicks off a computation in the background will be well understood by the users, then you might want to "hide" it in order to have cleaner code.
          – scrwtp
          Nov 23 at 14:00


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53434764%2ff-run-blocking-call-on-another-thread-use-in-async-workflow%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

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo