3 ms·
There are many alternatives to "fire-and-forget message passing" that JavaScript does, that don't require shared data structures or mutexes. JavaScript can imp
by geocar 11y ago
There are many alternatives to "fire-and-forget message passing" that JavaScript does, that don't require shared data structures or mutexes.
JavaScript can implement some of these alternatives with it's postMessage/onmessage combination, but it requires cooperation on both the sender and receiver which (in my opinion) is a limitation: The supervisor/operating system can expose just a little bit more information and it means the system isn't equally unresponsive anymore. Two that I'm thinking about are:
• Mailboxes. A process can post a message to a remote buffer and also check the fullness of that buffer so it can make other arrangements (e.g. scheduling another worker, letting the user know that the system is busy, etc). JavaScript can simulate this if both sides cooperate by implementing an ack-on-receive. UNIX allows detection of a full-buffer (EWOULDBLOCK). KDB publishes[1] the number of bytes in the output buffers which would also be preferable to what JavaScript does.
• Bulletin Boards. A process can simply publish information in a local buffer. Workers can then connect to pick up tasks. Again, JavaScript can simulate this if both sides cooperate, and implement a get/put system with postMessage/onmessage, and while this does represent a shared data structure, it's read-sharing which doesn't require any mutual-exclusion. You can also build it into a network protocol: My cexec[2] does this because load+network latency gives me free scheduling. Many mail servers also use this trick- one process writes to the queue directory, and workers pick up new work whenever they have time.
If you're interested in inter-process communication, Tanenbaum gave good writeup[3] on these methods (which I think are specifically relevant), and some other methods (which are useful in other circumstances).
[1]: http://code.kx.com/wiki/Reference/dotzdotW http://code.kx.com/wiki/Reference/dotzdotW
[2]: https://github.com/geocar/cexec https://github.com/geocar/cexec
[3]: http://www.amazon.co.uk/Modern-Operating-Systems-Andrew-Tanenbaum/dp/0138134596 http://www.amazon.co.uk/Modern-Operating-Systems-Andrew-Tane...
- Too 11y agoGood points, multiple workers on a single shared queue aren't exactly convenient with just a one way postMessage.