The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
As per this documentation: https://docs.scala-lang.org/overviews/core/futures.html
Await.results() should block until all futures have completed. This works as expected when called in an individual cell in a Fabric Notebook. However, when called within a loop or if present in a function and called through the function- the same doesn't work.
As you can see below things work fine if called within a cell:
However if called within a function (or in a loop)- the same doesn't work.
Solved! Go to Solution.
Hi @anawast ,
I have successfully reproduced your scenario and observed the behavior of Await.result() inside loops. Below is the output I received when executing the modified version of your code:
Key Findings:
Solution & Workaround:
To ensure Await.result() fully blocks execution inside a loop, you can use the following approach:
Code:
import scala.util.control.Breaks._
import java.time.LocalDateTime
import java.time.Duration
import scala.concurrent.{Future, Await}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
val start = LocalDateTime.now()
val threshold_executionMinutes = 1 // Adjust as needed
breakable {
while (true) {
val now: LocalDateTime = LocalDateTime.now()
val duration = Duration.between(start, now)
val executedMinutes = duration.toMinutes
if (executedMinutes >= threshold_executionMinutes) {
println("Execution threshold reached. Exiting loop.")
break()
}
val notebooks = Seq("Notebook_X", "Notebook_Y", "Notebook_Z")
println(" Awaiting notebook execution...")
val result = Await.result(Future.sequence(notebooks.map { notebook =>
Future {
println(s"Running notebook: $notebook")
Thread.sleep(5000) // Simulate notebook execution
s"Completed: $notebook"
}
}), scala.concurrent.duration.Duration.Inf) // Ensure full blocking
println(s"Iteration complete. Result: $result")
println(" Sleeping for 10 seconds before next iteration...\n")
Thread.sleep(10000) // Ensure loop doesn’t execute too fast
}
}
Expected Behavior:
Please try this solution in your Fabric Notebook and inform me if you continue to encounter issues. If this resolves your problem, kindly Accept it as a solution and give a "Kudos" to assist other members in finding it more easily.
I hope this works for you.
Thank you and Regards,
Menaka.
Hi @anawast ,
May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.
Thank you.
Hi @anawast ,
I hope this information is helpful. Please let me know if you have any further questions or if you'd like to discuss this further. If this answers your question, please Accept it as a solution and give it a 'Kudos' so others can find it easily.
Thank you.
Hi @anawast ,
I have successfully reproduced your scenario and observed the behavior of Await.result() inside loops. Below is the output I received when executing the modified version of your code:
Key Findings:
Solution & Workaround:
To ensure Await.result() fully blocks execution inside a loop, you can use the following approach:
Code:
import scala.util.control.Breaks._
import java.time.LocalDateTime
import java.time.Duration
import scala.concurrent.{Future, Await}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
val start = LocalDateTime.now()
val threshold_executionMinutes = 1 // Adjust as needed
breakable {
while (true) {
val now: LocalDateTime = LocalDateTime.now()
val duration = Duration.between(start, now)
val executedMinutes = duration.toMinutes
if (executedMinutes >= threshold_executionMinutes) {
println("Execution threshold reached. Exiting loop.")
break()
}
val notebooks = Seq("Notebook_X", "Notebook_Y", "Notebook_Z")
println(" Awaiting notebook execution...")
val result = Await.result(Future.sequence(notebooks.map { notebook =>
Future {
println(s"Running notebook: $notebook")
Thread.sleep(5000) // Simulate notebook execution
s"Completed: $notebook"
}
}), scala.concurrent.duration.Duration.Inf) // Ensure full blocking
println(s"Iteration complete. Result: $result")
println(" Sleeping for 10 seconds before next iteration...\n")
Thread.sleep(10000) // Ensure loop doesn’t execute too fast
}
}
Expected Behavior:
Please try this solution in your Fabric Notebook and inform me if you continue to encounter issues. If this resolves your problem, kindly Accept it as a solution and give a "Kudos" to assist other members in finding it more easily.
I hope this works for you.
Thank you and Regards,
Menaka.
Hi @anawast ,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a Kudos so other members can easily find it.
Thank you and Regards,
Menaka
Hi @anawast ,
Thank you for reaching out to us on the Microsoft Fabric Community Forum
Await.result blocks the thread until the Future completes. When used within a loop or a function, this blocking behavior can lead to inefficiencies and potential deadlocks, especially if multiple threads are involved.I suggest using Avoid Blocking in Loops/Functions, Instead of using Await.result within a loop or a function.
If this post was helpful, please give us Kudos and consider marking Accept as solution to assist other members in finding it more easily.
Regards,
Menaka.
User | Count |
---|---|
15 | |
9 | |
5 | |
3 | |
2 |
User | Count |
---|---|
45 | |
23 | |
17 | |
13 | |
12 |