i wonder is there any API or hack to increase apps Process Priority on Raspberry Pi2 for make sure there is no IO change missed. And should i busy wait for time sensetive jobs?
You may consider using Async and Await in your code.
If you specify that a method is an async method by using an Async or async modifier, you enable the following two capabilities.
The marked async method can use Await or await to designate
suspension points. The await operator tells the compiler that the
async method can't continue past that point until the awaited
asynchronous process is complete. In the meantime, control returns
to the caller of the async method.
The suspension of an async method at an await expression doesn't constitute an exit from the method, and finally blocks don’t run.
The marked async method can itself be awaited by methods that call it.
You can learn more from here: https://msdn.microsoft.com/en-us/library/hh191443.aspx
Related
I am creating an Application for Kiosk Devices which is running the API recursively and updating the data on Screen, but I realized after some days the App got Crashed due to a recursive function it's a StackOverflow Error.
What is the solution to avoid this error or the best approach to Call Infinite Recurising API with delay?
Following is the Approach, I am using in my app
CallRestApi(){
await getResponseFromServer();
updateUi();
await Future.delayed(Duration(seconds: 2));
CallRestApi();
}
Edit: I also used the timer approach but result is same
For starters, you could make your call non-recursive by using a simple loop instead of recursion:
Future<void> CallRestApi() async{
while(true) {
await getResponseFromServer();
updateUi();
await Future.delayed(Duration(seconds: 2));
}
}
That still is suspicious and probably not the best way to do this.
Now I don't know your use case, but you may want to look into a few options to get rid of this for good:
Background tasks: How to schedule background tasks in Flutter?
Timer periodic: How do I run a reoccurring function, in Dart?
Stream periodic: Dart Documentation
In Flutter we use async await and Future, can someone explain that if we don't use another thread (which we can't in dart) and run the job on main UIThread only won't the app become choppy because even if we are waiting for the job to execute it will ultimately execute on UIThread only.
I somewhere read about isolates also. But cannot paint the exact picture. If someone can explain in detail.
I think you missed something when it comes to asynchronous methods in Dart.
Dart is a single-threaded programming language, Java or C# are multi-threaded programming languages, forget async as a new thread, it doesn't happen in Dart.
Dart is a single-threaded programming language. This means is that Dart can only run one instruction at a time, while Java could run multiple instructions concurrently.
As a rule, everything you do in Dart will start in UI-Thread. Whatever method you call in Dart, whether using sync, async, then, they will be running on UI-Thread, since Dart is a single thread.
In single-threaded languages like Javascript and Dart, an async method is NOT executed in parallel but following the regular sequence of events, handled by the Event Loop. There are some problems (I would give some approaches, as we will see below) if you run the following code in a multithreaded language where fetch will take some time to execute:
String user = new Database.fetch(David);
String personalData = new Database.fetch(user);
You will receive David's data in user, and after that, you will receive your data.
This will lock your UI, unlike languages like Java which have Threads, where you can perform this task in the background, on another thread, and the UI-Thread will run smoothly.
If you do this at Dart
String user = new Database.fetch(David);
String personalData = new Database.fetch(user);
user will be null in personalData, because the fetch event is a Future.
How to solve this in Dart?
String user = await Database.fetch(David);
String personalData = await Database.fetch(user);
For those who like a more functional paradigm (I don't like it) you can use then.
Database.fetch(David).then((user){
Database.fetch(user).then((personal){
String personalData = personal;
});
});
However, imagine that you have billions of data in that database, this heavy task will probably cause the animations on your screen to freeze, and you will see a jank in the user's UI, for that purpose isolates were invented.
Dart Isolates give you a way to perform real multi-threading in Dart. They have their own separate heaps(memory), and run the code in the background, just like the Threads of multi-threaded languages. I could explain how isolates work, but it would make this response very long, and the goal is just to differentiate asynchronous from multi-threaded methods.
A simple way to solve the problem above using isolates would be using compute.
Compute was created to facilitate the creation of isolates, you just pass the function and the data that this function will execute, and that's it!
Important to remember that compute is a Future, so you have to use await or then to get its result.
In our example, we could create a new thread and get its result when we finish by just calling compute like this:
String user = await compute(Database.fetch,David);
String personalData = await compute(Database.fetch,user);
Very simple, isn't it?
In summary:
Everything that waits some time to be completed, in Dart is called a "Future".
To wait for the result of a future to be assigned to a variable, use await or then.
The asynchronous methods (await and then) can be used to obtain a result from a Future, and are executed ON THE MAIN THREAD because Dart is single-thread.
If you want to run any function on a new thread, you can create an isolate. Dart offers an easy-to-use isolate wrapper called compute, where you only need to pass one method that will be processed and the data that will be processed, and it will return its result in the future.
NOTE: if you are going to use compute make sure you are using a static or top-level method (see that in the example I used Database.fetch it was no accident if you need to call Database().fetch or need to create an instance of it, means it is not a static method and will not work with isolates).
English is not my first language and I didn't want to write so much because of that, but I hope I helped differentiate between multi-threaded asynchronous programming from single-threaded asynchronous programming.
I can't find an async version of RemoveRange. It exists, for example, for AddRange. Anyone knows why? It seems odd to me not to have an homogeneous set of commands.
Because it is synchronous operation and providing fake Async method which runs synchronously and returns completed task would be misleading and against async method principles.
EF Core provides async versions only for methods which potentially access database - e.g. Add{Range}, Find, SaveChanges, Dispose, and sync only version for methods which operate purely on state (change tracker) like Attach{Range}, Update{Range}, Remove{Range}.
As of why Add{Range} have async version, the reason is explained in the documentation:
This method is async only to allow special value generators, such as the one used by Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo, to access the database asynchronously.
The example of isolate in flutter! does not work. the new isolate does not run. but if I remove the async of entry point function. it works fine. So, How can I make await operation in a new isolate in flutter.
There might be a Dart 2 bug.
If async doesn't work, you can always use .then() instead of async and await to chain async calls.
had the same problem whilst moving code from the main isolate to another one. in my case i move async method channel calls which stopped working afterwards in the new isolate. in the main isolate the calls blocked the ui.
found the following
https://github.com/flutter/flutter/issues/13937
https://github.com/flutter/flutter/issues/16846
I am using play framework , i read that Play handles every request in non blocking way.
So what is the difference between Blocking & Non Blocking Future in Scala?
Also please provide information about Future & Await.Result() method
Thanks !!!
If Await.Result() is called at any point before the Future has completed, the Future becomes blocking. If you instead use onComplete, onSuccess, onFailure, map, or flatMap (and some other methods), you are registering a callback function that will occur when the Future returns. Thus, the Future is non-blocking. Use non-blocking Futures with callbacks whenever possible.