Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Building the Station Summary Dashboard

Building the Station Summary Dashboard

In this lesson, you build the station summary dashboard: one worker per station, and three lines to print. Ridge with 30 from three samples, Harbor with 10 from two, and a total of 40. Can JavaScript print these three lines, in this order, with every worker gone before the total?

The Python version is the reference your port has to match. summarize() adds up a station’s samples and counts them. summarize_station() runs it in a process pool and puts the station’s id in front. And main() gathers both stations, then prints each row and the grand total:

Language: Python
import asyncio
from concurrent.futures import ProcessPoolExecutor

STATIONS = [
    {"id": "Ridge", "samples": [8, 12, 10]},
    {"id": "Harbor", "samples": [4, 6]},
]

def summarize(samples):
    return {"total": sum(samples), "count": len(samples)}

async def summarize_station(pool, station):
    loop = asyncio.get_running_loop()
    summary = await loop.run_in_executor(
        pool, summarize, station["samples"]
    )
    return {"id": station["id"], **summary}

async def main():
    with ProcessPoolExecutor() as pool:
        rows = await asyncio.gather(
            *(summarize_station(pool, s) for s in STATIONS)
        )
    for row in rows:
        print(row)
    total = sum(row["total"] for row in rows)
    print("total", total)

if __name__ == "__main__":
    asyncio.run(main())

In JavaScript, the work moves to worker.mjs, as before. It sums workerData with .reduce() and now posts a count too, workerData.length, like Python’s len():

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll build the station summary dashboard: one worker per station, and three lines to print. Ridge, thirty from three samples. Harbor, ten from two.

00:11 A total of forty. Can JavaScript print these three lines, in this order, with every worker gone before the total?

00:21 Here’s the Python version your port has to match. summarize() adds up a station’s samples and counts them. summarize_station() runs it in a process pool and puts the station’s id in front.

00:34 And main() gathers both stations, then prints each row and the grand total.

00:40 In JavaScript, the work moves to worker.mjs, as before. It sums workerData with .reduce(), and now it posts a count too: workerData.length, like Python’s len().

00:54 runWorker() is your helper, unchanged, now exported from a module of its own. It starts one worker and settles on the worker’s exit event, so it resolves with the message only after the worker has exited.

01:09 Now the dashboard itself, dashboard.mjs. It imports runWorker(), and STATIONS holds the same two stations as Python’s list.

01:20 summarizeStation() is an async function, like Python’s async def. It awaits runWorker() with the station’s samples, where Python awaits run_in_executor(): both give you something to await for one job’s result.

01:33 The analogy stops at the pool. A pool keeps its processes for the next job, while each runWorker() call starts a fresh worker that exits when it’s done.

01:44 Then it returns a new object: the id first, then the summary’s fields, spread in with three dots, the way **summary unpacks a dictionary.

01:55 main() gathers both stations with asyncio.gather(). In JavaScript, .map() turns each station into a promise of its summary, and Promise.all() waits for all of them.

02:07 .map() calls summarizeStation() for both stations right away, and each call runs up to its first await, so both workers start before Promise.all() waits.

02:16 Python’s coroutines don’t start until main() awaits gather(); either way, both jobs run at once. Top-level await stands in for asyncio.run(): JavaScript’s event loop is already running your module.

02:32 Python’s with block shuts the pool down and waits for its processes. There’s no pool here: each runWorker() settles only after its worker exits, so when Promise.all() resolves, both workers have exited.

02:46 The dashboard starts them once, two stations and two workers, never inside a poll loop.

02:52 Here’s where it stops matching: if one station’s job threw, Promise.all() would reject at once, without waiting for the other worker. Nothing catches it, so no rows print: the program ends, and the other worker stops unfinished.

03:07 gather() raises at once too, but Python’s with block waits for the other job before the error comes out.

03:15 Next, a for...of loop logs each row, like Python’s for loop. Say Harbor’s worker finishes first. Which row does the loop log first?

03:25 Your turn: pause the video and predict.

03:34 Ridge’s row. Promise.all() fills its array in input order, whichever worker finishes first, as asyncio.gather() does. Responses can still arrive in any order, as you saw with stale readings.

03:49 But the array’s order comes from STATIONS.

03:54 Last, the grand total. Python sums a generator over the rows. .reduce() does the same, starting from zero and adding each row’s total. Now run both programs.

04:06 JavaScript prints the three lines you set out to get, in order, and Python’s three match them: Ridge with thirty from three samples, Harbor with ten from two, and a total of forty.

04:18 Only the way each language prints a dictionary or an object differs.

04:24 So the dashboard starts one worker per station with runWorker(), waits for all of them with Promise.all(), and gets its rows in input order.

04:34 Every worker has exited before the totals print.

04:38 Next up is the exercise Finish the End-of-Shift Dashboard. There, you’ll summarize two more stations, label one that has no samples, and wire in the end-of-shift stop.

Become a Member to join the conversation.