F# run blocking call on another thread, use in async workflow
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#
add a comment |
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#
add a comment |
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#
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#
asynchronous f#
asked Nov 22 '18 at 16:10
sdgfsdhsdgfsdh
8,18283995
8,18283995
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
}
How should I rewriteasyncFoo
such that I don't have to remember to doAsync.StartChild
on each call?
– sdgfsdh
Nov 23 '18 at 11:31
Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing theAsync.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 '18 at 14:00
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
}
How should I rewriteasyncFoo
such that I don't have to remember to doAsync.StartChild
on each call?
– sdgfsdh
Nov 23 '18 at 11:31
Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing theAsync.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 '18 at 14:00
add a comment |
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
}
How should I rewriteasyncFoo
such that I don't have to remember to doAsync.StartChild
on each call?
– sdgfsdh
Nov 23 '18 at 11:31
Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing theAsync.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 '18 at 14:00
add a comment |
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
}
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
}
answered Nov 22 '18 at 17:01
scrwtpscrwtp
11.4k11626
11.4k11626
How should I rewriteasyncFoo
such that I don't have to remember to doAsync.StartChild
on each call?
– sdgfsdh
Nov 23 '18 at 11:31
Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing theAsync.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 '18 at 14:00
add a comment |
How should I rewriteasyncFoo
such that I don't have to remember to doAsync.StartChild
on each call?
– sdgfsdh
Nov 23 '18 at 11:31
Do what makes sense in the context of your codebase. As a rule of thumb I would prefer seeing theAsync.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 '18 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 '18 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 '18 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 '18 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 '18 at 14:00
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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