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?
asynchronous f#
add a comment |
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?
asynchronous f#
add a comment |
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?
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 at 16:10
sdgfsdh
7,79673789
7,79673789
add a comment |
add a comment |
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
}
How should I rewriteasyncFoosuch that I don't have to remember to doAsync.StartChildon 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 theAsync.StartChildon 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
add a comment |
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
}
How should I rewriteasyncFoosuch that I don't have to remember to doAsync.StartChildon 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 theAsync.StartChildon 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
add a comment |
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
}
How should I rewriteasyncFoosuch that I don't have to remember to doAsync.StartChildon 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 theAsync.StartChildon 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
add a comment |
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
}
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 at 17:01
scrwtp
11.1k11626
11.1k11626
How should I rewriteasyncFoosuch that I don't have to remember to doAsync.StartChildon 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 theAsync.StartChildon 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
add a comment |
How should I rewriteasyncFoosuch that I don't have to remember to doAsync.StartChildon 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 theAsync.StartChildon 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
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.
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.
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