Why is my infinite loop blocking when it is in an async function?
The async
keyword, and promises in general, don't make synchronous code asynchronous, slow running code fast, or blocking code non-blocking.
async
just makes the function return a promise and provides (with the await
keyword) a mechanism to interact with other promises as if there were synchronous.
Your function starts a loop, and then just goes around and around.
It doesn't get to the end of the function, which would end the function and resolve the promise it returned.
It doesn't reach an await
keyword and pause while it waits for the awaited promise to be resolved.
It just goes around and around.
If you were actually doing something in the loop which was computationally expensive and you wanted to push off into the background, then you could use a Node.js Worker Thread or a browser-based Web Worker to do it.
Why is async function blocking?
JavaScript isn't really multithreaded. It relies on two things to give that illusion:
Computers are really really fast and generally complete whatever needs to be done instantaneously as far as a human can tell
Because they are fast and the world is slow, computers spend a lot of time waiting for something to happen, and can profitably do something else in the meantime
Basically, JavaScript runs a thread until it either finishes, or is waiting for something to happen (e.g. an async network call), at which point JavaScript will switch to another thread, if there is one.
Your first sleep()
function is a busy wait. It consumes 100% CPU doing nothing. More importantly, it never gives another thread a chance to run. The promise solution does it the JavaScript way: it ends almost immediately (thereby giving other threads a chance to run) and uses SetTimeout
to regain control (in an async sort of way) when other threads aren't running.
Is it possible to write an asynchronous loop inside synchronous function?
The code with the async
block is unreachable since an async
block does not execute the code inside, rather it creates a future that when will be .await
ed will execute the code inside.
To execute an asynchronous code inside a synchronous function you need to block. The functions for that are usually provided by the async runtime you use. For example, tokio has block_on()
(and a bunch of other functions). You do it like:
tokio::runtime::Runtime::new().unwrap().handle().block_on(async {
// ...
});
// Or by re-using the current runtime, if you have an active one:
tokio::runtime::Handle::current().block_on(async {
// ...
});
There is a runtime-agnostic block_on()
function provided by the futures
crate, although using it is likely to be less performant and/or interfere with the runtime.
Note that blocking while you're executing asynchronous code (if the outer function is called by async code) is bad and may have serious implications.
Async Infinite Loop
and after that infinite loop...
That's not really what "infinite" means :)
You can have two simultaneous running processes, simply put them in their own threads. This would also have the added benefit that your application host isn't blocked by your loop, so you'd be free to do other things with the application host. (And it doesn't appear to be "frozen" to the host system.)
It could be something as simple as this...
public class Worker
{
public void DoWork()
{
while (true)
{
alive(); // this sends an http request to my server
Thread.Sleep(5000); // sleep for 5 seconds avoiding computer overcharge
}
}
}
Then to start it as a thread:
var workerObject = new Worker();
var workerThread = new Thread(workerObject.DoWork);
workerThread.Start();
You can send a variety of information and signals to threads to provide them with data from the consuming code and to control them while they're running. (Since you certainly wouldn't want runaway threads that you can't interact with.) So this is just a very simple example to get you started and illustrate the concept.
You can find a lot more information about and more complete examples on MSDN, among a number of other places.
Related Topics
How to Get Current Value of Rxjs Subject or Observable
How to Implement Authenticated Routes in React Router 4
Memory Leak Risk in JavaScript Closures
Date Constructor Returns Nan in Ie, But Works in Firefox and Chrome
Image Loaded Event in for Ng-Src in Angularjs
How to Get the HTML for a Dom Element in JavaScript
Phonegap Inappbrowser Display PDF 2.7.0
Generic Way to Detect If HTML Form Is Edited
Detect Input Value Change with Mutationobserver
JSON Object Undefined in Internet Explorer 8
Jquery Checkbox Change and Click Event
Get Hours Difference Between Two Dates in Moment Js
Why Do Results Vary Based on Curly Brace Placement
How to Get Jquery.Trigger('Click'); to Initiate a Mouse Click
Get Value of Input Field Inside an Iframe