Search code examples
async-awaitdartblockingnonblocking

Interleaved execution of while loops in the Dart language using async and await


Please demonstrate in a simple dart code snippet that while loops that perform calculations (e.g. optimization or model fitting) that are under internal control (other than operations controlled from the opaque outer context like file access) can be run in interleaved way.

My hypothesis is that it is not possible unless one runs a blocking instruction within each while loop like file access or the use web sockets.

The documentation on asynchronous programming in dart does not mention the term "blocking" in that sense. The documentation on asynchronous programming in vertx (java application server library) does. But I dont know if the latter can be generalized since platforms could implement different intentions about asynchronous programming.


Solution

  • I'm not sure what your question is. Your hypothesis, if I understand it correctly, is correct.

    Dart is single-threaded inside each isolate. Only one thing in each isolate can be executing at a time.

    That does mean that a long calculation that never does an asynchronous operation, won't interleave with other calculations.

    If the computation does asynchronous operations (which means waiting for something else), then something else does get to run.