SD 212 Spring 2024 / Homeworks


hw23: Reading about multiprocessing vs threading

  • Due before the beginning of class on Monday, March 25

Today’s homework is a reading on two approaches to concurrency in Python: multiprocessing (which should already be familiar to you from class already) and multithreading (which is a different concept we will talk about next).

After reading, fill in the short markdown file to confirm and check your understanding.

Reading

Because concurrency is not really well covered in our usual textbooks, you are going to read a blog post instead. I think the post actually does a terrific job of talking about these two approaches in the context of data scientist and hope you will enjoy it too!

Here it is: Multiprocessing vs Threading in Python by Sumit Ghosh.

Questions

  1. Did you read the entire post?

    (Answer yes or no)

  2. Check out the following function. It uses a global variable to add to a running sum:

    sum = 0
    
    def addtosum(n):
        sum += n

    What is the correct syntax to create a new thread to run this function with input n=25?

    1. t1 = threading.Thread(addtosum, 25,)
    2. t1 = threading.Thread(target=addtosum, args=(25,))
    3. t1 = threading.Thread(target=addtosum(25),)
    4. t1 = threading.Thread(target=addtosum)(25)

    (Just enter the letter of the correct choice.)

  3. The previous example function addtosum works with threading but not with multiprocessing. Why not?

    1. GIL
    2. Race condition
    3. Shared memory
    4. Deadlock
    5. Akshually it does work
  4. What is one thing in this reading that you’re still unsure/confused about and would like to discuss more in class?

Submit command

To submit files for this homework, run one of these commands:

submit -c=sd212 -p=hw23 hw23.md 
club -csd212 -phw23 hw23.md
Download the file hw23.md to fill in and submit for this homework