Difference between Thread, Isolate and Process in Dart - flutter

What's the difference between Thread, Isolate and a Process in Dart?
As far as I know Dart is a single-threaded language, but it can spawn many isolates which don't share memories with each other and we can do the heavy lifting work on them and return the result without blocking the UI.
But what Process is for, is that a part of an Isolate? Can anyone describe above three in more detail.
And when we do asynchronous programming using Future and let's see we are doing heavy lifting in it, will that block the UI thread in case it is awaited using the await keyword.

A Process is a native OS (Unix, Windows, MacOS) construct, which consists of one or more threads with their own address space and execution environment. In Dart, an application consists of one or more threads, one of which is the main UI thread, while the rest are typically called Isolates.

In contrast to what the previous answer said, All Dart code runs in an isolate.
Actually, your understanding is correct, based on what you said in the comments.
From the docs (emphasis mine):
Within an app, all Dart code runs in an isolate. Each Dart isolate has
a single thread of execution and shares no mutable objects with other
isolates. To communicate with each other, isolates use message
passing. Although Dart’s isolate model is built with underlying
primitives such as processes and threads that the operating system
provides, the Dart VM’s use of these primitives is an implementation
detail that this page doesn’t discuss.
Many Dart apps use only one isolate (the main isolate), but you can
create additional isolates, enabling parallel code execution on
multiple processor cores.
Your question hasn't been answered yet, and it will not be answered easily, because it depends on the language implementation.
Also, your another question: "And when we do asynchronous programming using Future and let's see we are doing heavy lifting in it, will that block the UI thread in case it is awaited using the await keyword."
-> Yes, it will block the UI, unless you use another isolate.

Related

Dart/Flutter inspect Future "stack traces"

Sometimes I encounter a deadlock, or something that resembles deadlock. When I debug code in most languages, I'll usually pause the program and inspect the state of all the running threads, to see e.g. what the thread that was SUPPOSED to pick up the phone is doing instead. (E.g. via the Debugger > Frames panel in Android Studio.) In Flutter, however, you don't really use threads, you use Futures, and I haven't found a way to get similar information. I'd like a way to see all living Futures and where they're waiting in the code. (In addition to the location each Isolate is executing, I guess, and the Futures categorized by Isolate.) Is there such a thing? Is such a feature planned? (It should be, IMO.)

Difference between Isolate vs Thread vs Process?

As we know flutter app runs in an isolates. Somewhere I read that isolates are not system processes. So what really an isolate is and how it's different from process.
Good question. But there is no definite answer, because it depends on the language implementation.
Let me just quote the docs (emphasis mine):
Within an app, all Dart code runs in an isolate. Each Dart isolate has
a single thread of execution and shares no mutable objects with other
isolates. To communicate with each other, isolates use message
passing. Although Dart’s isolate model is built with underlying
primitives such as processes and threads that the operating system
provides, the Dart VM’s use of these primitives is an implementation
detail that this page doesn’t discuss.
Many Dart apps use only one isolate (the main isolate), but you can
create additional isolates, enabling parallel code execution on
multiple processor cores.
As you might know, threads and processes are managed by OS.
Isolates are managed by Dart runtime, and they use underlying OS threads and processes.
The Dart runtime controls the main isolate (where code normally runs)
and any other isolates that the app creates.

Flutter & Dart : What is an isolate in flutter?

As I study about flutter, I notices that there is a thing called isolate.
What is it for? And how do we implement that? Can you give me a simple example?
Thank you in advance.
Isolate in flutter, similar with threading.
"Flutter is single-threaded but it is capable of doing multi-threading
stuff using Isolates (many processes). When Dart starts, there will be
one main Isolate(Thread). This is the main executing thread of the
application, also referred to as the UI Thread. In simple Flutter apps
you will only ever use one Isolate, and your app will run smoothly.
Isolates are:
Dart’s version of Threads. Do not share memory between each other.
Uses Ports and Messages to communicate between them. May use another
processor core if available. Runs code in parallel."
Docs & Simple Example
"Concurrent programming using isolates: independent workers that are
similar to threads but don't share memory, communicating only via
messages."
Official Docs

Asynchronous Computation in scalajs Diode

I have an user interface and provide a button to the user, which executes the function longComputation(x: A): A and updates then the user interface (particularly the model) with the new result. This function may take longer to compute the result and should therefore compute in parallel.
Diode provides me with Effect, PotAction, and AsyncAction. I read the documentation about Effects and PotActions/AsyncActions, but I cannot even get a simple example to work.
Can someone point me to or provide an simple working example?
I created a ScalaFiddle based on the SimpleCounter example. There is a LongComputation button, which should run in parallel; but is not.
In JavaScript you cannot run things in parallel without using Web Workers because the JS engine is single-threaded. Web Workers are more like separate processes than threads, as they don't share memory and you need to send messages to communicate between workers and the main thread.
Have less than 50 reputation to comment, so I have to create a new answer instead of commenting #ochrons answer:
As mentioned Web Workers communicate via message passing and share no state. This concept is somehow similar to Akka - even Akka.js exists which enables you to use actor systems in ScalaJS and therefore the browser.

Non-blocking / Asynchronous Execution in Perl

Is there a way to implement non-blocking / asynchronous execution (without fork()'ing) in Perl?
I used to be a Python developer for many years... Python has really great 'Twisted' framework that allows to do so (using DEFERREDs. When I ran search to see if there is anything in Perl to do the same, I came across POE framework - which seemed "close" enough to what I was searching for. But... after spending some time reading the documentation and "playing" with the code, I came against "the wall" - which is following limitation (from POE::Session documentation):
Callbacks are not preemptive. As long as one is running, no others will be dispatched. This is known as cooperative multitasking. Each session must cooperate by returning to the central dispatching kernel.
This limitation essentially defeats the purpose of asynchronous/parallel/non-blocking execution - by restricting to only one callback (block of code) executing at any given moment. No other callback can start running while another is already running!
So... is there any way in Perl to implement multi-tasking (parallel, non-blocking, asynchronous execution of code) without fork()'ing - similar to DEFERREDs in Python?
Coro is a mix between POE and threads. From reading its CPAN documentation, I think that IO::Async does real asynchronous execution. threads can be used too - at least Padre IDE successfully uses them.
I'm not very familiar with Twisted or POE, but basic parallel execution is pretty simple with threads. Interpreters are generally not compiled with threading support, so you would need to check for that. The forks package is a drop-in replacement for threading (implements the full API) but using processes seamlessly. Then you can do stuff like this:
my $thread = async {
print "you can pass a block of code as an arg unlike Python :p";
return some_func();
};
my $result = $thread->join();
I've definitely implemented callbacks from an event loop in an async process using forks and I don't see why it wouldn't work with threads.
Twisted also uses cooperative multi-tasking just like POE & Coro.
However it looks like Twisted Deferred does (or can) make use of threads. NB. See this answer from the SO question Twisted: Making code non-blocking
So you would need to go the same route with POE (though using fork is probably preferable).
So one POE solution would be to use: POE::Wheel::Run - portably run blocking code and programs in subprocesses.
For alternatives to POE take a look at AnyEvent and Reflex.
I believe you use select for that kind of thing. More similarly to forking, there's threading.
POE is fine if you want asynchronous processing but using only a single cpu (core) is fine.
For example if the app is I/O limited a single process will be enough most of the time.
No other callback can start running while another is already running!
As far as I can tell - this is the same with all languages (per CPU thread of course; modern web servers usually spawn a least one process or thread per CPU core, so it will look (to users) like stuff it working in parallel, but the long-running callback didn't get interrupted, some other core just did that work).
You can't interrupt an interrupt, unless the interrupted interrupt has been programmed specifically to accommodate it.
Imagine code that takes 1min to run, and a PC with 16 cores - now imagine a million people try to load that page, you can deliver working results to 16 people, and "time out" all the rest, or, you can crash your web server and give no results to anyone. Folks choose not to crash their web server, which is why they never permit callbacks to interrupt other callbacks (not that they could even if they tried - the caller never gets control back to make a new call before the prior one has ended anyhow...)