I was reading through turtle crate and I came across this function remote_handle
I viewed the docs for remote_handle but was unable to understand it.
I was able to understand the first line. But was unable to understand the 2nd line.
Turn this future into a future that yields () on completion and sends its output to another future on a separate task.
This can be used with spawning executors to easily retrieve the result of a future executing on a separate task or thread.
This method is only available when the std feature of this library is activated, and it is activated by default.
My question - Can anyone explain the 2nd line with the help of a small example?
I am trying to understand why it was used in the turtle crate when starting up the backend server.
Say you have some quite heavy workload that you therefore want to run on a separate task, to allow it to run in a different thread. But this workload also has a result, and you want to retrieve this result from the original task (or a different task). You can spawn a task and execute the future there, and send the result back to the original thread with a oneshot channel.
remote_handle()
basically abstracts over this. It takes a future which returns something, and gives you back a future that you can execute on the new task (that returns()
), and a handle (also a future) that can be.await
ed on the original task, and that will give you the result of the future after it finished executing on the other task (of course, you need to.await
it).