Skip to content

Instantly share code, notes, and snippets.

@gr33ngr33n
Forked from montanaflynn/CONCURRENCY.md
Created January 17, 2021 08:08
Show Gist options
  • Select an option

  • Save gr33ngr33n/50a2fa627926fa6b84778fb7df36beb2 to your computer and use it in GitHub Desktop.

Select an option

Save gr33ngr33n/50a2fa627926fa6b84778fb7df36beb2 to your computer and use it in GitHub Desktop.

Revisions

  1. Montana Flynn revised this gist Aug 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ delayedLog(2, 1000)
    ```

    Now you might be wondering what setTimeout is doing and it's basically saying
    Run this function after this much time. Javascript is a Higher-order programming
    run this function after this much time. Javascript is a Higher-order programming
    language which means you can pass functions as arguments. The first delayedLog
    will print to stdout after 2 seconds. The second one will print after 1 second.

  2. Montana Flynn revised this gist Aug 11, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -70,8 +70,8 @@ finally in parallel with the help of the cluster module.
    For reference here is what I mean by sequential, concurrent and parallel:

    - *Sequential*: do this and then do that
    - *Concurrent*: do this and do that without waiting for either
    - *Parallel*: do this and do that at the same time
    - *Concurrent*: do this and do that without waiting between
    - *Parallel*: do this and do that at the exact same time

    ```js
    // send the request with a request-id header read the body
  3. Montana Flynn revised this gist Aug 11, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ Logging from function call #2
    ```

    But that is not the case, both will be fired at the same time one after
    the other and the output would instead be like this:
    the other so the output would be like this:

    ```
    // ...wait for 1 second
  4. Montana Flynn revised this gist Aug 11, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ Logging from function call #1
    Logging from function call #2
    ```

    But that is not the case. Instead both will be fired at the same time one after
    But that is not the case, both will be fired at the same time one after
    the other and the output would instead be like this:

    ```
  5. Montana Flynn revised this gist Aug 11, 2016. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,12 @@
    Javascript is a programming language with a peculiar twist. Its event driven
    model means that nothing blocks and everything runs concurrently. This is not
    to be confused with the same type of concurrency as running in parallel on multiple
    cores. Javascript is single threaded so each program runs on a single core.

    Every line of code executes without waiting for anything to return. This sounds
    cores. Javascript is single threaded so each program runs on a single core yet
    every line of code executes without waiting for anything to return. This sounds
    weird but it's true. If you want to have any type of sequential ordering you can
    use events, callbacks, or as of late promises. Let's see an example:
    use events, callbacks, or as of late promises.

    Let's see an example:

    ```js
    function delayedLog(index, delay){
    @@ -24,6 +25,7 @@ Now you might be wondering what setTimeout is doing and it's basically saying
    Run this function after this much time. Javascript is a Higher-order programming
    language which means you can pass functions as arguments. The first delayedLog
    will print to stdout after 2 seconds. The second one will print after 1 second.

    What you might expect to see is:

    ```
  6. Montana Flynn revised this gist Aug 11, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    Javascript is a programming language with a peculiar twist. Its event driven
    model means that nothing blocks and everything runs concurrently. This is not
    to be confused with the same type of concurrency as running in parallel on multiple
    cores. Javascript is single threaded so each program runs core on one core.
    cores. Javascript is single threaded so each program runs on a single core.

    Every line of code executes without waiting for anything to return. This sounds
    weird but it's true. If you want to have any type of sequential ordering you can
  7. Montana Flynn revised this gist Aug 10, 2016. No changes.
  8. Montana Flynn revised this gist Aug 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Concurrency in JavaScript

    Javascript is a programming language with a peculiar twist. It's event driven
    Javascript is a programming language with a peculiar twist. Its event driven
    model means that nothing blocks and everything runs concurrently. This is not
    to be confused with the same type of concurrency as running in parallel on multiple
    cores. Javascript is single threaded so each program runs core on one core.
  9. Montana Flynn revised this gist Aug 10, 2016. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -65,6 +65,12 @@ function that sends multiple HTTP requests, first by the function which takes
    a callback and then invoking that function sequentially, concurrently and
    finally in parallel with the help of the cluster module.

    For reference here is what I mean by sequential, concurrent and parallel:

    - *Sequential*: do this and then do that
    - *Concurrent*: do this and do that without waiting for either
    - *Parallel*: do this and do that at the same time

    ```js
    // send the request with a request-id header read the body
    // simulated delays in responses between .3 and .6 seconds
    @@ -86,11 +92,6 @@ function sendRequest(requestID, cb){
    }).end()
    }


    // Sequential = do this and then do that
    // Concurrent = do this and do that without waiting for either
    // Parallel = do this and do that at the same time

    // SEQUENTIAL REQUESTS
    // Have to try a little bit
    sequentialRequests(5)
    @@ -105,7 +106,6 @@ function sequentialRequests(count, i){
    })
    }


    // CONCURRENT REQUESTS
    // Default by nature of js
    concurrentRequests(5)
    @@ -162,7 +162,6 @@ function parallizedRequests(count){
    }
    ```


    ### Further Resources

    - https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop
  10. Montana Flynn revised this gist Aug 10, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,6 @@ model means that nothing blocks and everything runs concurrently. This is not
    to be confused with the same type of concurrency as running in parallel on multiple
    cores. Javascript is single threaded so each program runs core on one core.

    - *Sequential*: do this and then do that after you're done
    - *Concurrent*: do this and do that and don't wait for either one to be done before starting the next
    - *Parallel*: do this and do that at the same time

    Every line of code executes without waiting for anything to return. This sounds
    weird but it's true. If you want to have any type of sequential ordering you can
    use events, callbacks, or as of late promises. Let's see an example:
    @@ -91,6 +87,10 @@ function sendRequest(requestID, cb){
    }


    // Sequential = do this and then do that
    // Concurrent = do this and do that without waiting for either
    // Parallel = do this and do that at the same time

    // SEQUENTIAL REQUESTS
    // Have to try a little bit
    sequentialRequests(5)
  11. Montana Flynn revised this gist Aug 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    Javascript is a programming language with a peculiar twist. It's event driven
    model means that nothing blocks and everything runs concurrently. This is not
    to be confused with the type of concurrency as running in parallel on multiple
    to be confused with the same type of concurrency as running in parallel on multiple
    cores. Javascript is single threaded so each program runs core on one core.

    - *Sequential*: do this and then do that after you're done
  12. Montana Flynn revised this gist Aug 10, 2016. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,9 @@ model means that nothing blocks and everything runs concurrently. This is not
    to be confused with the type of concurrency as running in parallel on multiple
    cores. Javascript is single threaded so each program runs core on one core.

    - *Sequential*: do this and then do that after you're done
    - *Concurrent*: do this and do that and don't wait for either one to be done before starting the next
    - *Parallel*: do this and do that at the same time

    Every line of code executes without waiting for anything to return. This sounds
    weird but it's true. If you want to have any type of sequential ordering you can
    @@ -66,7 +69,7 @@ function that sends multiple HTTP requests, first by the function which takes
    a callback and then invoking that function sequentially, concurrently and
    finally in parallel with the help of the cluster module.

    ```
    ```js
    // send the request with a request-id header read the body
    // simulated delays in responses between .3 and .6 seconds
    function sendRequest(requestID, cb){
    @@ -158,3 +161,10 @@ function parallizedRequests(count){
    }
    }
    ```


    ### Further Resources

    - https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop
    - http://bytearcher.com/articles/parallel-vs-concurrent/
    - http://nickwritesablog.com/the-state-of-concurrent-javascript/
  13. Montana Flynn revised this gist Aug 10, 2016. 1 changed file with 114 additions and 0 deletions.
    114 changes: 114 additions & 0 deletions CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@ model means that nothing blocks and everything runs concurrently. This is not
    to be confused with the type of concurrency as running in parallel on multiple
    cores. Javascript is single threaded so each program runs core on one core.


    Every line of code executes without waiting for anything to return. This sounds
    weird but it's true. If you want to have any type of sequential ordering you can
    use events, callbacks, or as of late promises. Let's see an example:
    @@ -43,4 +44,117 @@ Logging from function call #2
    Logging from function call #1
    ```

    The most traditional way of handling concurrency where you needed to run functions
    in a certain order is known as callbacks. This is when you pass a function to a
    be run at a later time and given arguments that can be acted upon then. That is
    what setTimeout is doing when you pass it a function.

    ```js
    function log(err, msg) {
    if (err) panic(err)
    console.log(msg)
    }

    function doSomething(arg, callback) {
    if (isError()) callback("An error happened", null)
    setTimeout(callback(null, arg.reverse), 1000)
    }
    ```

    Now let's look at some real world examples of concurrency. We'll be making a
    function that sends multiple HTTP requests, first by the function which takes
    a callback and then invoking that function sequentially, concurrently and
    finally in parallel with the help of the cluster module.

    ```
    // send the request with a request-id header read the body
    // simulated delays in responses between .3 and .6 seconds
    function sendRequest(requestID, cb){
    var delay = Math.floor(Math.random() * (6 - 3 + 1)) + 5
    var request = {
    hostname: 'httpbin.org',
    headers: {'request-id': requestID},
    path: '/delay/.'+ delay
    }
    require('http').get(request, (res) => {
    var body = ''
    res.on('data', function (chunk) {
    body += chunk
    })
    res.on('end', function () {
    cb(res, body)
    })
    }).end()
    }
    // SEQUENTIAL REQUESTS
    // Have to try a little bit
    sequentialRequests(5)
    function sequentialRequests(count, i){
    if (i == undefined) {i = 0}
    if (i++ >= count) {return}
    sendRequest(i, function(res, body){
    var reqID = JSON.parse(body).headers['Request-Id']
    var sCode = res.statusCode
    console.log("Sequential Response #"+reqID+" returned a "+sCode)
    sequentialRequests(count, i)
    })
    }
    // CONCURRENT REQUESTS
    // Default by nature of js
    concurrentRequests(5)
    function concurrentRequests(limit){
    for (var i = 0; i < limit; i++) {
    sendRequest(i, function(res, body){
    var reqID = JSON.parse(body).headers['Request-Id']
    var sCode = res.statusCode
    console.log("Concurrent Response #"+reqID+" returned a "+sCode)
    })
    }
    }
    // PARALLEL REQUESTS
    // pretty complex and for not much gain
    parallizedRequests(5)
    function parallizedRequests(count){
    var cluster = require('cluster')
    var count = count
    if(cluster.isMaster) {
    for(var i = 0; i < count; i++) {
    var worker = cluster.fork()
    worker.on('message', function(message) {
    console.log(message.data.result)
    })
    }
    var i = 0
    for(var wid in cluster.workers) {
    i++
    if (i > count) {return}
    cluster.workers[wid].send({
    type: 'request',
    data: {
    number: i
    }
    })
    }
    } else {
    process.on('message', function(message) {
    if(message.type === 'request') {
    sendRequest(message.data.number, function(res,body){
    var reqID = JSON.parse(body).headers['Request-Id']
    var sCode = res.statusCode
    process.send({
    data: {
    result: "Parallel Response #"+reqID+" returned a "+sCode
    }
    })
    process.exit(0)
    })
    }
    })
    }
    }
    ```
  14. Montana Flynn revised this gist Aug 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Concurrency in JavaScript
    # Concurrency in JavaScript

    Javascript is a programming language with a peculiar twist. It's event driven
    model means that nothing blocks and everything runs concurrently. This is not
  15. Montana Flynn revised this gist Aug 10, 2016. 1 changed file with 46 additions and 0 deletions.
    46 changes: 46 additions & 0 deletions CONCURRENCY.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // Concurrency in JavaScript

    Javascript is a programming language with a peculiar twist. It's event driven
    model means that nothing blocks and everything runs concurrently. This is not
    to be confused with the type of concurrency as running in parallel on multiple
    cores. Javascript is single threaded so each program runs core on one core.

    Every line of code executes without waiting for anything to return. This sounds
    weird but it's true. If you want to have any type of sequential ordering you can
    use events, callbacks, or as of late promises. Let's see an example:

    ```js
    function delayedLog(index, delay){
    setTimeout(function(){
    console.log("Logging from function call #"+index)
    }, delay)
    }

    delayedLog(1, 2000)
    delayedLog(2, 1000)
    ```

    Now you might be wondering what setTimeout is doing and it's basically saying
    Run this function after this much time. Javascript is a Higher-order programming
    language which means you can pass functions as arguments. The first delayedLog
    will print to stdout after 2 seconds. The second one will print after 1 second.
    What you might expect to see is:

    ```
    // ...wait for 2 seconds
    Logging from function call #1
    // ...wait for 1 second
    Logging from function call #2
    ```

    But that is not the case. Instead both will be fired at the same time one after
    the other and the output would instead be like this:

    ```
    // ...wait for 1 second
    Logging from function call #2
    // ...wait for 2 seconds
    Logging from function call #1
    ```


  16. Montana Flynn revised this gist Aug 8, 2016. No changes.
  17. Montana Flynn revised this gist Aug 8, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions parallel.js
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,7 @@ function getResponse(i, cb){
    }

    if(cluster.isMaster) {
    console.log("Running concurrent requests!")
    var numWorkers = require('os').cpus().length;
    for(var i = 0; i < 5; i++) {
    var worker = cluster.fork()
  18. Montana Flynn revised this gist Aug 8, 2016. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions parallel.js
    Original file line number Diff line number Diff line change
    @@ -20,15 +20,15 @@ function getResponse(i, cb){
    }

    if(cluster.isMaster) {
    var numWorkers = require('os').cpus().length;
    for(var i = 0; i < 5; i++) {
    var worker = cluster.fork()
    worker.on('message', function(message) {
    console.log(message.data.result)
    })
    }
    var numWorkers = require('os').cpus().length;
    for(var i = 0; i < 5; i++) {
    var worker = cluster.fork()
    worker.on('message', function(message) {
    console.log(message.data.result)
    })
    }
    var count = 0
    for(var wid in cluster.workers) {
    for(var wid in cluster.workers) {
    count++
    if (count > 5) {return}
    cluster.workers[wid].send({
    @@ -37,18 +37,18 @@ if(cluster.isMaster) {
    number: count
    }
    })
    }
    }
    } else {
    process.on('message', function(message) {
    if(message.type === 'request') {
    process.on('message', function(message) {
    if(message.type === 'request') {
    getResponse(message.data.number, function(res){
    process.send({
    data: {
    result: res
    }
    })
    data: {
    result: res
    }
    })
    process.exit(0)
    })
    }
    })
    }
    }
    })
    }
  19. Montana Flynn revised this gist Aug 8, 2016. 3 changed files with 0 additions and 3 deletions.
    1 change: 0 additions & 1 deletion concurrent.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    // CONCURRENT REQUESTS
    // Default by nature
    var http = require('http')

    function concurrentRequests(){
    1 change: 0 additions & 1 deletion parallel.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    // PARALLEL REQUESTS
    // pretty complex
    var http = require('http')
    var cluster = require('cluster')

    1 change: 0 additions & 1 deletion sequential.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    // SEQUENTIAL REQUESTS
    // Have to try
    var http = require('http')

    function sequentialRequest(count){
  20. Montana Flynn revised this gist Aug 8, 2016. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions concurrent.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    var http = require('http')

    // CONCURRENT REQUESTS
    // Default by nature
    var http = require('http')

    function concurrentRequests(){
    for (var i = 0; i < 5; i++) {
    var request = {
    4 changes: 2 additions & 2 deletions sequential.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    var http = require('http')

    // SEQUENTIAL REQUESTS
    // Have to try
    var http = require('http')

    function sequentialRequest(count){
    if (count == undefined) {count = 0}
    count++
  21. Montana Flynn revised this gist Aug 8, 2016. 1 changed file with 55 additions and 0 deletions.
    55 changes: 55 additions & 0 deletions parallel.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    // PARALLEL REQUESTS
    // pretty complex
    var http = require('http')
    var cluster = require('cluster')

    function getResponse(i, cb){
    var request = {
    hostname: 'httpbin.org',
    headers: {'request-id': i},
    path: '/delay/.'+ Math.floor(Math.random() * (5 - 1 + 1)) + 1
    }
    http.get(request, (res) => {
    var body = ''
    res.on('data', function (chunk) {
    body += chunk
    })
    res.on('end', function () {
    cb(JSON.parse(body).headers['Request-Id'])
    })
    }).end()
    }

    if(cluster.isMaster) {
    var numWorkers = require('os').cpus().length;
    for(var i = 0; i < 5; i++) {
    var worker = cluster.fork()
    worker.on('message', function(message) {
    console.log(message.data.result)
    })
    }
    var count = 0
    for(var wid in cluster.workers) {
    count++
    if (count > 5) {return}
    cluster.workers[wid].send({
    type: 'request',
    data: {
    number: count
    }
    })
    }
    } else {
    process.on('message', function(message) {
    if(message.type === 'request') {
    getResponse(message.data.number, function(res){
    process.send({
    data: {
    result: res
    }
    })
    process.exit(0)
    })
    }
    })
    }
  22. Montana Flynn revised this gist Aug 7, 2016. 2 changed files with 26 additions and 25 deletions.
    25 changes: 25 additions & 0 deletions concurrent.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    var http = require('http')

    // CONCURRENT REQUESTS
    // Default by nature
    function concurrentRequests(){
    for (var i = 0; i < 5; i++) {
    var request = {
    hostname: 'httpbin.org',
    headers: {'request-id': i},
    path: '/delay/.'+ Math.floor(Math.random() * (5 - 1 + 1)) + 1
    }
    http.get(request, (res) => {
    var body = ''
    res.on('data', function (chunk) {
    body += chunk
    })
    res.on('end', function () {
    console.log(JSON.parse(body).headers['Request-Id'])
    })
    }).end()
    }
    }

    console.log("Running concurrent requests!")
    concurrentRequests()
    26 changes: 1 addition & 25 deletions concurrency.js → sequential.js
    Original file line number Diff line number Diff line change
    @@ -23,30 +23,6 @@ function sequentialRequest(count){
    }).end()
    }

    console.log("Running sequential requests :(")
    console.log("Running sequential requests!")
    sequentialRequest()


    // CONCURRENT REQUESTS
    // Default by nature
    function concurrentRequests(){
    for (var i = 0; i < 5; i++) {
    var request = {
    hostname: 'httpbin.org',
    headers: {'request-id': i},
    path: '/delay/.'+ Math.floor(Math.random() * (5 - 1 + 1)) + 1
    }
    http.get(request, (res) => {
    var body = ''
    res.on('data', function (chunk) {
    body += chunk
    })
    res.on('end', function () {
    console.log(JSON.parse(body).headers['Request-Id'])
    })
    }).end()
    }
    }

    console.log("Running concurrent requests!")
    concurrentRequests()
  23. Montana Flynn created this gist Aug 7, 2016.
    52 changes: 52 additions & 0 deletions concurrency.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    var http = require('http')

    // SEQUENTIAL REQUESTS
    // Have to try
    function sequentialRequest(count){
    if (count == undefined) {count = 0}
    count++
    if (count > 5) {return}
    var request = {
    hostname: 'httpbin.org',
    headers: {'request-id': count},
    path: '/delay/.'+ Math.floor(Math.random() * (5 - 1 + 1)) + 1
    }
    http.get(request, (res) => {
    var body = ''
    res.on('data', function (chunk) {
    body += chunk
    })
    res.on('end', function () {
    console.log(JSON.parse(body).headers['Request-Id'])
    sequentialRequest(count)
    })
    }).end()
    }

    console.log("Running sequential requests :(")
    sequentialRequest()


    // CONCURRENT REQUESTS
    // Default by nature
    function concurrentRequests(){
    for (var i = 0; i < 5; i++) {
    var request = {
    hostname: 'httpbin.org',
    headers: {'request-id': i},
    path: '/delay/.'+ Math.floor(Math.random() * (5 - 1 + 1)) + 1
    }
    http.get(request, (res) => {
    var body = ''
    res.on('data', function (chunk) {
    body += chunk
    })
    res.on('end', function () {
    console.log(JSON.parse(body).headers['Request-Id'])
    })
    }).end()
    }
    }

    console.log("Running concurrent requests!")
    concurrentRequests()