Quiz

  1. Can you re-explain event loops?

    The event loop is the busy baker. The baker mixes cakes and puts them in the oven, one at a time. When the cakes finish, the baker pulls them out, frosts them, and puts them on the shelf.

    The baker can handle several cakes simultaneously, because there's a lot of time spent waiting.

  2. Could you explain how Node.js handles concurrency with its single-threaded event loop?

    I'm hoping the bakery metaphor will help. Node.js has a single thread, which does all the computation (there's not much) and just starts I/O events and responds when they finish.

  3. Could you elaborate on blocking/non-blocking? I'm unsure if I understand this.

    Sure. Blocking is what we are used to: for example, the Python input() function just waits as long as necessary for us to type in our string, and then it returns that string to our program and our program continues.

    
        def circle_area():
            radius = int(input('what is the radius?'))
            print('excellent!') # happens after the string is read
            area = math.pi * radius * radius
            print(f'the area of the circle is {area}')
    
    

    But with non-blocking I/O, our program doesn't wait (made-up syntax):

    
        def circle_area():
            input('what is the radius?', callback)
            print('excellent!')  # happens right away
    
        def callback(radius):
            radius = int(radius)
            area = math.pi * radius * radius
            print(f'the area of the circle is {area}')
    
    
  4. can you also talk a bit more about the asynchronous and synchronous i/o? is it a bad thing if the cpu is spending its time waiting?

    Well, the CPU is never waiting if there's some computation to be done.

    But threads can be waiting on I/O, and that can be a disadvantage.

    Programming with blocking I/O and threads is typically easier than programming with non-blocking I/O and an event-loop, but programming language support is getting better.

    I think in the long run, non-blocking I/O will become more common.

  5. Can we go over compute-bound vs I/O bound computation again?

    Glad to. Suppose, for some frequent computation, it takes 10ms to read some values from a file and 1ms to compute the answer. Your boss wants to improve the speed of the computation.

    For the same amount of money, you have a choice of

    1. buying a disk that is twice as fast or
    2. buying a processor that is twice as fast.

      Which should you do?

  6. Can you talk more about the differences in application between event driven and multithreaded?

    It has to do with this notion of compute-bound versus I/O-bound computation. I/O bound computations that use non-blocking I/O can be handled by fewer threads, which reduces memory demands, and therefore allows more concurrency.

    So, in the case where we want to maximize concurrency, event-driven programming can be an advantage.

    That's not always the case, but it often is with web applications.

  7. do you suggest taking 204 after taking 304? would content overlap? I remember that you mentioned 204 is mostly for beginners in coding and it is uncommon to take both at the same time.

    Good question!

    First, yes, CS 204 is for beginners. It doesn't even have CS 230 as a pre-req.

    Second, yes, there is some overlap. A good chunk of CS 304 covers front-end JS, which CS 204 also does. CS 204 does nothing in the back end, nothing with databases, and nothing with node.js. So there's still lots of interesting stuff to learn if you took both. Nevertheless, you would learn more by taking 204 with the fall version of CS 304 than with the spring version of 304.

  8. has anyone taken a 304 spring version after a fall version? I looked through the comparison page a lot of times but still find the version differences kind of confusing. Is there a version that is more popular? I know that you or someone else mentioned that flask and mysql has been around for longer.

    You can't take both versions of CS 304. The department decided to give them the same number specifically to make that clear.

    I'm happy to talk more about the differences. CS 304

    Both are very popular.

    Yes, the technology for the fall version is older than that of the spring version.

    But, to be very clear, both are widely used in industry. Neither is going away in the near future.