I'll Be Backend
Principles · Python

Hollywood Principle: stop polling, let the framework call you

Let Checkout tell each part about an order, so none is missed.

16 steps5 chapters~7 min at 1.25×#hollywood principle#observer#polling#listeners
The lesson plays right here. Pause anywhere, step back, or jump to a step below. Watch the Python video on YouTube Code on GitHub

Watch the narrated video

Every step

Problem

Steps 1–5

Refactor

Steps 6–11

Payoff

Step 12

Python bonus

Steps 13–14

Recap

Steps 15–16

The code: before and after

Before

class Checkout:
    def __init__(self) -> None:
        self._orders: list[int] = []

    def place(self, order_id: int) -> None:
        self._orders.append(order_id)

    def open_orders(self) -> list[int]:
        return self._orders


class Packing:
    def __init__(self) -> None:
        self.last_seen = 0

    def check(self, checkout: Checkout) -> None:
        open_ids = checkout.open_orders()
        for order_id in open_ids[self.last_seen:]:
            print(f"pack {order_id}")
        self.last_seen = len(open_ids)


class Invoicing:
    def __init__(self) -> None:
        self.last_seen = 0

    def check(self, checkout: Checkout) -> None:
        ids = checkout.open_orders()
        for order_id in ids[self.last_seen:]:
            print(f"invoice {order_id}")
        self.last_seen = len(ids)


def main() -> None:
    checkout = Checkout()
    packing = Packing()
    invoicing = Invoicing()
    # tick 1
    checkout.place(101)
    packing.check(checkout)
    invoicing.check(checkout)
    # tick 2
    checkout.place(102)
    packing.check(checkout)
    invoicing.check(checkout)
    # tick 3
    packing.check(checkout)
    invoicing.check(checkout)


main()

After

from collections.abc import Callable

OrderListener = Callable[[int], None]


class Checkout:
    def __init__(self) -> None:
        self._orders: list[int] = []
        self._listeners: list[OrderListener] = []

    def subscribe(self, listener: OrderListener) -> None:
        self._listeners.append(listener)

    def place(self, order_id: int) -> None:
        self._orders.append(order_id)
        for listener in self._listeners:
            listener(order_id)

    def cancel(self, order_id: int) -> None:
        self._orders.remove(order_id)


class Packing:
    def order_placed(self, order_id: int) -> None:
        print(f"pack {order_id}")


def invoice(order_id: int) -> None:
    print(f"invoice {order_id}")


def main() -> None:
    checkout = Checkout()
    packing = Packing()
    checkout.subscribe(packing.order_placed)
    checkout.subscribe(invoice)
    checkout.place(101)
    checkout.place(102)
    checkout.cancel(101)
    checkout.place(103)


main()

Transcript, step by step

Problem

Step 1: Two parts that poll Before

This is a shop's checkout, and placing an order appends its id to a list of open orders. Packing asks Checkout for that list, prints pack for every id from last seen on, then moves last seen to the end. Invoicing does the same, with invoice instead of pack. Main runs three ticks, and each tick may place an order, then lets both parts check for new ones. Tick three places nothing, so both checks find nothing new, and the output is pack and invoice for 101 and 102. Both parts poll Checkout here, asking it each tick whether anything is new. Checkout knows nothing about packing or invoicing, and asking each tick was the shortest way to find new orders.

Step 2: Customers can cancel Change request

Now customers can cancel an order, and a cancelled order leaves the open list. The quick edit adds a cancel method to Checkout, which removes that id from the list. Tick three now cancels order 101 and places order 103, then lets packing and invoicing check as before. Mypy passes, so what does tick three print? Nothing at all, so order 103 is never packed and never invoiced, and no error tells us.

Step 3: A guess from the length Code smell

After the cancel and the new order, the list is two long and last seen is two, so the slice is empty. Each part polls Checkout and guesses what's new from the list's length. So when a cancel and a new order land between two polls, they hide each other from that guess. You can see it in Packing's loop here, the same loop in Invoicing here, and the cancel that breaks it here. Order 103 is never packed and never invoiced, yet the program runs without a single error. And the same guess lives in each part, so a fix has to be made in every poller.

Step 4: Let the source call Principle

We could fix the guess in both loops, but every new poller would have to repeat that fix. Only Checkout knows the moment an order exists, so Checkout should call each part at that moment. Low-level parts don't call the source to ask, the source calls them, and that rule is the Hollywood Principle. Its short form is don't call us, we'll call you. So now: Checkout must call each part, and no part asks.

Step 5: Refactor first Back it out

So we take cancel back out, and tick three is an empty poll again. We refactor first in small steps, with the same four lines of output each time, and cancel comes back at the end.

Refactor

Step 6: Packing's work per order Extract Method

So in Packing, we extract the print into its own method, order placed, which takes one order id. The loop now calls order placed for each new id, and the output is the same.

Step 7: Invoicing's work per order Extract Method

Invoicing gets the same move, with its invoice print in its own order placed method. Its loop calls that method too, and the output still has the same four lines.

Step 8: Name the listener Extract Interface

Both methods have the same shape, so we extract an interface, a Protocol named Order Listener, with that one method. Packing and Invoicing don't inherit from it, because a Protocol matches any class that has that method. Nothing calls them through Order Listener yet, so the output is the same.

Step 9: Checkout calls Packing Apply Observer

Now Checkout keeps a list of listeners, and its subscribe method appends one to that list. After place appends the id, it calls order placed on every listener, and that call is a notification. Checkout calls listeners it only knows through one Protocol, and that design is the Observer pattern. In that pattern, Checkout plays the Subject and Packing plays an Observer. Main subscribes Packing and stops calling its check, while mypy confirms that Packing matches Order Listener. Each tick still prints pack before invoice, because place calls Packing before main calls the invoicing check.

Step 10: Checkout calls Invoicing Apply Observer

Invoicing gets the same move, so main subscribes it and stops calling its check. Now no part asks Checkout anything, because Checkout calls each listener from the loop in place. Invoicing is an Observer too, and the output is still the same four lines.

Step 11: Delete the polling Remove Dead Code

Both check methods, both last seen fields, and open orders had no callers left. So we delete them, and main shrinks to two subscribes and two orders, with the same four lines out.

Payoff

Step 12: Cancel, take two Payoff

So now cancel comes back, the same method in Checkout as before. Main places 101 and 102, then cancels 101 and places 103, just like the old tick three. What does the output say about order 103 this time? Pack 103 and invoice 103, because place calls each listener the moment the order exists. Packing and Invoicing didn't change at all. Where Packing's loop and Invoicing's loop guessed, there's no loop now, and cancel can't hide 103 anymore. The parts stopped calling Checkout and Checkout calls them, just as the Hollywood Principle says. But to follow one order now, you read the loop in place and then every listener it calls.

Python bonus

Step 13: The listener as a Callable Python bonus

But Invoicing is a whole class, three lines long, for one print line. In Python a function is a value, so Order Listener can become a Callable that takes an int and returns None. Place now calls each listener directly, and main subscribes the bound method order placed of each object.

Step 14: A listener as a function Python bonus

So the Invoicing class becomes a plain function, invoice, and main subscribes it by name. Mypy checks that invoice takes an int, and the output is the same, because place calls it like any listener. Once a listener needs its own state, a class like Packing is still the right home for it.

Recap

Step 15: Where we started, where we ended Before / after

On the left, Packing and Invoicing each polled Checkout and guessed new orders from the list's length. So a cancel between two polls could hide a brand-new order from both of them. On the right, Checkout calls every listener the moment an order is placed. And each listener, even the plain function, only says what happens to one order.

Step 16: When to reach for it Takeaways

Reach for it when several parts must react to something that happens in one place. If nothing can call you back, like an outside API with no webhook, you have to keep asking. Spot it by a counter like last seen, or a loop that asks the source whether anything is new. Python's own exit works this way, because atexit calls every function you register when the program ends.