-
Notifications
You must be signed in to change notification settings - Fork 2
Asynchronous vs Synchronous
When in programming you deal with input and output (eg. I/O) you fatally end up with the choice of how do you process the data ?
Asynchronous or Non-Blocking is a form of processing I/O in such a way that when you start to process one set of data you do not have to wait for this processing to end to deal with another set of data.
Synchronous or Blocking is a simpler form of processing I/O which when you start to access a set of data you have to wait for it to complete before accessing another set of data.
For example: moving 3 files from A to B
synchronous / blocking
move file1
wait ...
file1 moved
move file2
wait ...
file2 moved
move file3
wait ...
file3 moved
asynchronous / non-blocking
move file1
move file2
move file3
wait ...
file2 moved
file1 moved
file3 moved
On the command line you have access to standard streams:
- Standard Input or stdin
- Standard Output or stdout
- Standard Error or stderr
(it's like a 2nd stdout reserved only for errors)
It can be illustrated with those few commands
$ ls > file_list.txt
$ ls >> file_list.txt
$ sort < file_list.txt
$ sort < file_list.txt > sorted_file_list.txt
$ ls -1 | less
see: LinuxCommand.org I/O Redirection
Same as with the command line but instead we use socket to transport the data.
With sockets:
- you read the input with
recv()
- you write to the output with
send()
We are still in the same logic: reading input and writing output.
This I/O happen over the sockets to transfer "Hyper Text" between a server and a client.
A Server listen for client requests (input) and answer to them with responses (output).
A client emit requests (output) towards the server and expect in returns responses (input).
When you apply all that to HTTP you can talk about the Request-Response cycle.
For each request the server receive
- the server launch a process
- then pass the request to this process input
- then wait for the process output or to terminate
- then send back the output to the client
You launch first a program that basically run in an endless loop, either this program receive data and process it or it stays here sleeping.
Let's call this program a daemon.
For each request the server receive
- server pass it to the daemon input
- the daemon mark this connection with the socket ID
- the server disconnect (or is free to pass again more requests)
Because this daemon is always running, in one of its loop, when he is done processing that request, it will use the socket ID to send back the output to the client.
TODO
explain Apache prefork / workers
see: Apache MPM worker
see: Understanding Apache 2 MPM
see: Apache: multi-threaded vs multi-process
TODO
explain Node.js event loop
see: Understanding the node.js event loop
see: Node.js introduction for synchronous guys
see: From PHP to Node.js