1 of 25

Understanding Asynchronous Execution: �Processes, Threads, and Asyncio

CSCI 338: Software Engineering

Fall 2026

2 of 25

Tuesday

3 of 25

Agenda

  1. Asynchronous logic (Tuesday)
  2. Activity (Tuesday)
  3. Intro to MVC (Thursday)

4 of 25

What do we want to know about the game industry?

Brainstorm questions (see notes below)

5 of 25

Before we begin, �review the Activity Instructions and �pull down the latest files

​

6 of 25

Introduction to Asynchronous Execution

What is it?

  • Non-blocking way to execute computations, in parallel.

What benefits does it provide?

  • it frees your computer up to field other requests.

7 of 25

Two categories of tasks to think about

CPU-Bound Tasks: Limited by the processing power of the CPU. Bottleneck: CPU resources.

  • What are some examples of this kind of task?

�I/O-Bound Tasks: Limited by the speed of input/output operations, like reading from a disk, making network requests, or querying a database. Bottleneck: waiting for external resources.

  • What are some examples of this kind of task?

8 of 25

1. Processes

Use when you want true CPU parallelism for computationally expensive tasks.

​

Pros:

  • True parallel execution (leverages multiple CPU cores)
  • Memory isolation prevents accidental overwrites of data; locking mechanism to avoid race conditions.

Cons:

  • Higher overhead (process creation and communication are expensive)
  • Can have race conditions if you don’t do things correctly.

9 of 25

Demo

(asyncio-activity/processes_race_condition.py)

10 of 25

Let’s talk about what just happened…

When multiple processes or threads access shared data simultaneously without proper synchronization, they can interfere with each other in unexpected ways. This is called a race condition:

  1. Process A reads the current value (e.g., counter = 5)
  2. Process B reads the same value (counter = 5)
  3. Process A increments and writes back (counter = 6)
  4. Process B increments and writes back (counter = 6) ❌

Result: Both processes thought they incremented the counter, but it only increased by 1 instead of 2!

11 of 25

Avoid race conditions by using locks

  • Use synchronization mechanisms like locks to ensure only one process can access the shared data at a time.
  • Race conditions are one of the most common and tricky bugs in concurrent programming because they're often intermittent and hard to reproduce consistently!

12 of 25

Threads: I/O Bound Tasks

Lightweight execution units sharing the same memory space.

Rule of thumb: only use if asyncio is not available

Pros:

  • Lower overhead than processes
  • Suitable for I/O-bound tasks

Cons:

  • Still more complicated than asyncio (next slide)
  • But some languages don’t support asyncio, so you’ll need to use threads.

13 of 25

Asyncio and Coroutines: I/O Bound Tasks

Async I/O with coroutines (single-threaded, cooperative multitasking)

Pros:

  • Ideal for I/O-bound tasks (e.g., network requests, database calls)
  • Low overhead, as it doesn’t require multiple threads or processes

Cons:

  • Not suitable for CPU-bound tasks
  • Requires an event loop and async-aware libraries

14 of 25

When to use what?

Use Processes when:

  • You need true parallel execution
  • Your task is CPU-bound (e.g., image processing, machine learning)

Use Asyncio when:

  • Your task is I/O-bound (network requests, file I/O)
  • You need high scalability with lightweight coroutines

Use Threads when:

  • Your task is I/O-bound and requires shared memory
  • AsyncIO isn’t available

​

15 of 25

AsyncIO Concepts: The Event Loop

The event loop continuously monitors and manages the execution of asynchronous tasks in a program.

  • Think of it as a conductor in an orchestra, coordinating multiple musicians (tasks) to ensure they play in sync without waiting on each other unnecessarily.

16 of 25

AsyncIO Concepts: The Event Loop

  • Schedules Tasks: When you create asynchronous tasks, the event loop decides when each task gets to run.�
  • Handles Waiting Efficiently: If a task needs to wait (e.g., await asyncio.sleep(2)), the event loop doesn’t just sit idle — it switches to other tasks while waiting, making better use of time.�
  • Manages Execution Flow: Repeatedly checks for tasks that are ready to run, runs them until they either finish or hit an await, and repeats this process until all tasks are done.

17 of 25

AsyncIO Concepts: Coroutines

async def prepare_drink(customer, drink):

# fake logic to prepare a drink

​

customers = [

("Alice", "Latte"),

("Bob", "Espresso"),

("Charlie", "Cappuccino")

]

tasks = []

for customer, drink in customers:

tasks.append(prepare_drink(customer, drink))

# Then, wait for all tasks to complete

await asyncio.gather(*tasks)

The for loop creates an array of coroutines – but does not invoke the functions yet.

​

await asyncio.gather(*tasks) �tells asyncio to schedule all those coroutines to run concurrently, and then wait until all of them complete.

18 of 25

19 of 25

Tuesday: Intro to MVC

20 of 25

Career Feedback

21 of 25

Introduction to the MVC Design Pattern

MVC is a design pattern that separates an application into three interconnected components:

  • Model: Manages the data (e.g., fetching Courses, weather data, student information, cat videos, etc.).
  • View: Handles the presentation layer (e.g., HTML templates).
  • Controller: Orchestrates / coordinates the flow of data between the Model and View (e.g., FastAPI routes).

This separation makes the code easier to manage and scale. Let’s walk through the code to understand how these components work together.

22 of 25

1. Model: Organizes & Fetches Data

  • Typically represent data fetched from a database that you manage yourself.
  • Since we’ve not covered databases yet, we’ll be fetching data from a bunch of different cloud providers.
  • Let’s take a look at app/models.py.

Each model represents one item of a data collection (in this case a Course class). But you could have other models (classes) too:

  • Instructor class
  • Department class
  • ….

Note that models can have helper methods too.

​

23 of 25

2. Controller: Orchestrates / Coordinates Requests

server.py file acts as the controller.

  • Anything with a @app.get(...) decorator is accessible over the web (think of it like a public function that you invoke by accessing a particular URL address). Web functions functions:
    • Receive HTTP requests (via web URLs)
    • Fetch data using model functions
    • Return a response back to the caller
  • The other functions in this file are helper functions

24 of 25

3. View: Displaying Data

The templates/index.html file displays data to the user (ideally in a simple, useable way).

  • This template is a combination of “vanilla” HTML and python expressions (surrounded by double curly braces).
  • Note that only keys that were passed into the template are available to the template.

​

25 of 25

  • Please take 20 minutes to complete the steps up to the end of 3.1. Run the App on your own.
  • We will work through 3.2. Trace a Request as a class