Dart Asynchronous Programming

I relate to java on how to do thread / async. I am using the new Thread (target) .start (), where target is Runnable as one way to do threading in java. New parallel api have alternatives, but we know that with a specific call new threads are executed, which are created and transmitted in tasks.

Just like asynchronously done in dart? I read about send / receivport, add-on / future, spawnFunction. For me, only spawnFunction is a convincing statement that will create a new thread. can explain how fullness / future help. I know that they accept callbacks, but there is some kind of implicit logic / rule in javascript / dart that callbacks are always executed on different threads.

below is a dart fragment / pseudo-code:

void callback() { print("callback called"); } costlyQuery(sql, void f()) { executeSql(sql); f(); } costlyQuery("select * from dual", callback); 

I hope that my expensive Qeery signature to accept a function as a second parameter is correct. so now I don’t think that f() after executeSql(sql) will be asynchronous. maybe above the example add add-on / future if it can do asynchronous to help me understand.

+4
source share
1 answer

tl; dr : There is no implicit rule that the callback will not block.

Javascript Event Queue

There are no threads in Javascript (except for WebWorkers, but these are different). This means that if any part of your code blocks is blocked, the entire application is blocked. There is nothing magical about callbacks, these are just functions:

 function longLoop(cb) { var i = 1000000; while (i--) ; cb(); } function callback() { console.log("Hello world"); } function fun() { longLoop(callback); console.log("Called after Hello World is printed"); } 

To make something asynchronous, the callback must be placed in the event queue, which happens only through some API calls:

  • custom event handlers - clicks, keyboard, mouse
  • API Event Handlers - XmlHTTPRequest Callbacks, Linking to WebWorker
  • synchronization functions - setTimeout, setInterval

When an event is triggered, the callback is placed in the event queue, which must be completed when all other callbacks are completed. This means that no two lines of your code will ever be executed at the same time.

Futures

I assume that you at least stumbled upon this post about futures . If not, it reads well and comes straight from the mouth of horses.

In Javascript, you need to do some work to understand asynchronous code. There is even a Javascript library called futures for doing ordinary things like asynchronous loops and sequences (full disclosure, I personally worked with the author of this library).

The concept of the future is a promise made by a function that creates the future, that the future will be completed at some point along the way. If you are going to make some kind of asynchronous call, create a future, return it and keep your promise when the asynchronous call ends. From the blog post:

 Future<Results> costlyQuery() { var completer = new Completer(); database.query("SELECT * FROM giant_table", (results) { // when complete completer.complete(results); }); // this returns essentially immediately, // before query is finished return completer.future; } 

If database.query is a blocking call, the future will end immediately. This example assumes that database.query is a non-blocking call (async), so the future will be executed after this function exits. completer.complete will call any function passed to completer.then() with the specified arguments.

Your example, modified to be idiomatic and asynchronous:

 void callback() { print("callback called"); } costlyQuery(sql) { var completer = new Completer(); executeSql(sql, () => completer.complete()); return completer.future; } costlyQuery("select * from dual").then(callback); 

This uses futures asynchronously and correctly. You can pass your own callback function to costlyQuery as you did, and call it in the executeSql to achieve the same, but this is a Javascript way, not a Dart path.

isolates

Isolates are similar to Java threads, except that isolates are a concurrency model and threads are a parallelism model (see this SO question for more information on concurrency vs parallelism).

Dart differs in that the specification does not guarantee that everything will be executed in one thread. It only requires that different executable contexts do not have access to the same data. Each isolator has its own memory and can only transmit (for example, send data) to other isolators via send / receive ports.

These ports work similarly to Go channels if you are familiar.

Isolation is an isolated execution context that runs independently of other code. In Javascript terms, this means that it has its own event queue. See the Dart tour for a more complete introduction to isolates.

Let's say your executeSql is a blocking function. If we do not want to wait for its completion, we can load it into isolation:

 void callback() { print("callback called"); } void costlyQuery() { port.receive((sql, reply) { executeSql(sql); reply.send(); }); } main() { var sendPort = spawnFunction(costlyQuery); // .call does all the magic needed to communicate both directions sendPort.call("select * from dual").then(callback); print("Called before executeSql finishes"); } 

This code creates isolation, sends data to it, and then registers a callback when it is done. Even if executeSql blocks, main() will not necessarily block.

+9
source

Source: https://habr.com/ru/post/1447667/


All Articles