Quiz

  1. Do threads make up a process? Do threads ever share heap and code memory with processes?

    A process is a collection of threads (usually only one), so your second question is always true.

    Yes. Most processes have just one thread, but sophisticated ones like deployed Flask apps can have multiple threads.

  2. What does threads look like in code? I’m having a hard time visualizing how to apply threads to flask applications.

    In a deployed Flask app, the Flask infrastructure creates a new thread for each incoming web request. That thread then calls our code. Our code looks exactly the same as it always does, but now we have to make sure it is thread safe, because several threads might be executing our code at once.

    It may help to anthropomorphize this. Imagine a recipe (our code), being executed by multiple chefs at once. There are shared resources; maybe the oven. Can all the chefs work together?

  3. How do threads relate to transactions, and also requests?

    As I just said, each request is a different thread. To play well with each other, they may need to lock resources. They also need to think about sharing the database (tables). That's where transactions come in.

  4. I think I'm a little confused by the terminology. When you say each request should have its own database connection, are you specifically referring to when we use the request module outlined in the other part of this lecture? I'm honestly unsure about how we can ensure this in our code or what we shouldn't do in our code.

    No, this has nothing to do with the requests reading (which was single-threaded and didn't connect to the database). This is only an issue for our back-end code.

  5. These questions are from the producer-consumer blog: What is a Condition() object in Python? After Condition().wait() releases the consumer thread’s lock on the queue, where does the consumer acquire it again?

    A condition object is a lock that will wake up the thread that is waiting for the lock. When a thread asks for the resource and the resource is in use, the condition puts the thread to sleep and wakes it up when the resource becomes available.