3 ms·
> If you think in async terms, it is blocking your entire event loop for some executor I think I'm mis-understanding this. async doesn't block your entire even
by nox101 2y ago
> If you think in async terms, it is blocking your entire event loop for some executor
I think I'm mis-understanding this. async doesn't block your entire event loop. Other events continue to be processed. Trivial example
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
setInterval(() => { console.log(performance.now()); }, 1000);
console.log('before');
await wait(10000);
console.log('after');
Did I misunderstand your meaning of "blocking the entire event loop"?
- whilenot-dev 2y agoI think GP means that a synchronous blocking call in an async runtime would block and bring the executor (event loop) to a global halt. Think import { readFileSync } from 'node:fs'; async function main() { const data = readFileSync( '/etc/machine-id', { encoding: 'utf-8' }, ); console.log(data); } await main(); VS import { readFile } from 'node:fs'; async function main() { const data = await new Promise((resolve, _reject) => { readFile( '/etc/machine-id', { encoding: 'utf-8' }, (_err, data) => { resolve(data); }, ); }); console.log(data); } await main(); (no error handling to bring the point better across).
- whilenot-dev 2y agoTo add: This issue isn't that troublesome in languages like JavaScript where the runtime is event driven out-of-the-box (synchrounous APIs are considered the exception and suffixed with 'Sync' by convention), but it will become difficult to reason about in languages like rust and python where an event driven runtime is optionally choosen by the developer. Because now it's not enough to conceptually grep all awaiting calls to reason about any blocking code, but you'd also need to know if any other synchronous calls are implicitly awaiting in an underlying layer.
- jerf 2y agoJust to confirm, whilenot-dev had it correct. You can have more than one executor in some implementations (you know you do if you can keep more than one CPU busy), but in all the implementations you're certainly causing problems if you sync-block an executor for long periods of time, that's not what the system is for. By contrast, you want to open a thread/goroutine/Erlang process/Haskell spark/execution context in a threaded language and have it just bang away on numeric computation for a few days? Usually that's just fine, though if you've got a green-threading implementation, double-check first. Most green thread implementations seem to go through a period where that is not fine, but then eventually it is; Go for instance passed through that quite a few versions ago.
- concerndc1tizen 2y agoYour code is wrong. This is blocking synchronous code in JavaScript: while (true) {}