-
-
Save giorgiosironi/034b62b7e4d11e9285007a9b0a1e9717 to your computer and use it in GitHub Desktop.
Test for understanding how blocking works
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const log = (text) => { | |
console.log(new Date() + ' ' + text); | |
} | |
const util = require('util'); | |
const sleep = async ms => { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
}; | |
const asyncFunc1 = async () => { | |
log('f1 Start'); | |
await sleep(2000); | |
log('f1 End'); | |
}; | |
const asyncFunc2 = async () => { | |
log('f2 Start'); | |
await sleep(1000); | |
let fd = await util.promisify(fs.open)('/tmp/example.txt', 'w'); | |
var longString = (new Array(10*1024*1024)).join("x"); | |
syncFunc(fd, longString); | |
log('f2 End'); | |
}; | |
const syncFunc = async (fd, content) => { | |
console.log('### do something sync...'); | |
for (let i = 0; i < 1000; i++) { | |
fs.writeSync(fd, content); | |
} | |
}; | |
log('### Startin...'); | |
asyncFunc1(); | |
log('### do another job....'); | |
asyncFunc2(); | |
log('### Stoppin...'); |
Now try changing line 26...
from
const syncFunc = (fd, content)
to
const syncFunc = async (fd, content)
Updated, re-run:
$ node blocking-test.js
Thu Nov 14 2019 10:03:20 GMT+0000 (Greenwich Mean Time) ### Startin...
Thu Nov 14 2019 10:03:20 GMT+0000 (Greenwich Mean Time) f1 Start
Thu Nov 14 2019 10:03:20 GMT+0000 (Greenwich Mean Time) ### do another job....
Thu Nov 14 2019 10:03:20 GMT+0000 (Greenwich Mean Time) f2 Start
Thu Nov 14 2019 10:03:20 GMT+0000 (Greenwich Mean Time) ### Stoppin...
### do something sync...
Thu Nov 14 2019 10:03:48 GMT+0000 (Greenwich Mean Time) f2 End
Thu Nov 14 2019 10:03:48 GMT+0000 (Greenwich Mean Time) f1 End
Would need to put syncFunc
inside a setTimeout
but, since it uses synchronous IO, to my understanding it will always run to completion without being interrupted by other events.
This is mine:
$ node --version
v10.14.1
$ node main.js
Thu Nov 14 2019 09:50:27 GMT+0000 (Greenwich Mean Time) ### Startin...
Thu Nov 14 2019 09:50:27 GMT+0000 (Greenwich Mean Time) f1 Start
Thu Nov 14 2019 09:50:27 GMT+0000 (Greenwich Mean Time) ### do another job....
Thu Nov 14 2019 09:50:27 GMT+0000 (Greenwich Mean Time) f2 Start
Thu Nov 14 2019 09:50:27 GMT+0000 (Greenwich Mean Time) ### Stoppin...
Thu Nov 14 2019 09:50:29 GMT+0000 (Greenwich Mean Time) f1 End
### do something sync...
Thu Nov 14 2019 09:50:50 GMT+0000 (Greenwich Mean Time) f2 End
What version of node are you running?
$ node -v
v10.17.0
Did the semantics of sync IO functions change between Node versions? Sounds like a large backward compatibility break.
Upgraded, no change:
[10:21:58][giorgio@Newton:~]$ node -v
v12.13.0
[10:22:21][giorgio@Newton:~]$ node blocking-test.js
Thu Nov 14 2019 10:22:25 GMT+0000 (Greenwich Mean Time) ### Startin...
Thu Nov 14 2019 10:22:25 GMT+0000 (Greenwich Mean Time) f1 Start
Thu Nov 14 2019 10:22:25 GMT+0000 (Greenwich Mean Time) ### do another job....
Thu Nov 14 2019 10:22:25 GMT+0000 (Greenwich Mean Time) f2 Start
Thu Nov 14 2019 10:22:25 GMT+0000 (Greenwich Mean Time) ### Stoppin...
### do something sync...
Thu Nov 14 2019 10:22:58 GMT+0000 (Greenwich Mean Time) f2 End
Thu Nov 14 2019 10:22:58 GMT+0000 (Greenwich Mean Time) f1 End
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output (writes a huge file on disk):