Quiz
- What exactly is a thread in non-abstract terms? Is it a set of executable instructions? I feel like I only have a vague idea of what it is.
A thread is an entry in the operating system's table of, well, threads. Each entry consists of a program counter (AKA instruction counter, or PC) and the address of its stack.
When the current thread stops for I/O or its time is up, the OS switches to executing another thread.
In slightly less concrete terms, a thread is a location within a program and a stack for it to use for its computation.
- Could you please explain context switches a bit more? I am a little confused overall
When the OS switches from one thread to another, all the registers are obsolete, along with chunks of various caches, so it takes time for all those to be loaded with the right data for the new thread and the thread to be computing at top speed.
Humans are the same way! We're not as good at multi-tasking as we think we are.
- What are some examples of problems that are solved by implementing multi threading? Could multi threading be relevant to our projects?
Anytime you have two simultaneous computations within the same program, threads are great. They are perfect for web applications like ours. One thread deals with Alice's request for the home page, while another deals with Betty's search of the database, and another deals with Cathy's insertion of a new movie and ...
Since these are all independent, we usually don't have to worry about locking and such. But sometimes we do.
- Could we go over threads and how they handle an HTTP request
See above.
- Can we review the risks of threads? / Could you explain more about the risky features of threads? What actually happens if flask applications are not thread-safe?
- Can we go over an example implementing threads?
We will not be creating threads directly in this course, but the Flask infrastructure does for deployed apps. (Development mode is single-threaded.)
- Is there a limit to the number of threads an application can handle?
In principle. The Apache web software is typically configured with a max of 75 threads per process, with lots of processes.
- can you talk more about situations where a global readonly variable would be helpful? Thanks!
There was a project where they needed to display a menu of departments/majors on many pages. That's a lot of data, but changes very slowly. So, rather than read it from the database with every request, just put it all in a global variable and use that in rendering.