Skip to content

Commit eca771f

Browse files
committed
feat: refactor ticket booking scenarios into a reusable function
1 parent 900cfa0 commit eca771f

1 file changed

Lines changed: 39 additions & 59 deletions

File tree

experiments/row_level_locking_ticket_booking.py

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,42 @@ def book_ticket_with_lock(
7676
print(f"❌ {customer_name}: Booking failed - {e}")
7777

7878

79+
def run_scenarios(
80+
engine, ticket_id: int, target_function: callable, nowait: bool = False
81+
):
82+
# Reset ticket
83+
with Session(engine) as session:
84+
ticket = session.get(Ticket, ticket_id)
85+
ticket.status = TicketStatus.AVAILABLE
86+
session.commit()
87+
88+
if nowait:
89+
threads = [
90+
threading.Thread(
91+
target=target_function, args=(engine, ticket_id, "Alice", True)
92+
),
93+
threading.Thread(
94+
target=target_function, args=(engine, ticket_id, "Bob", True)
95+
),
96+
threading.Thread(
97+
target=target_function, args=(engine, ticket_id, "Charlie", True)
98+
),
99+
]
100+
else:
101+
threads = [
102+
threading.Thread(target=target_function, args=(engine, ticket_id, "Alice")),
103+
threading.Thread(target=target_function, args=(engine, ticket_id, "Bob")),
104+
threading.Thread(
105+
target=target_function, args=(engine, ticket_id, "Charlie")
106+
),
107+
]
108+
109+
for t in threads:
110+
t.start()
111+
for t in threads:
112+
t.join()
113+
114+
79115
def ticket_booking_demo():
80116
engine = create_engine("postgresql://develop:develop_secret@localhost:5432/develop")
81117
SQLModel.metadata.create_all(engine)
@@ -92,74 +128,18 @@ def ticket_booking_demo():
92128
print("Scenario 1: WITHOUT LOCK (Double Booking Risk)")
93129
print("=" * 60)
94130

95-
threads = [
96-
threading.Thread(
97-
target=book_ticket_without_lock, args=(engine, ticket_id, "Alice")
98-
),
99-
threading.Thread(
100-
target=book_ticket_without_lock, args=(engine, ticket_id, "Bob")
101-
),
102-
threading.Thread(
103-
target=book_ticket_without_lock, args=(engine, ticket_id, "Charlie")
104-
),
105-
]
106-
107-
for t in threads:
108-
t.start()
109-
for t in threads:
110-
t.join()
131+
run_scenarios(engine, ticket_id, book_ticket_without_lock)
111132

112133
print("\n" + "=" * 60)
113134
print("Scenario 2: WITH LOCK (Safe Booking)")
114135
print("=" * 60)
115136

116-
# Reset ticket
117-
with Session(engine) as session:
118-
ticket = session.get(Ticket, ticket_id)
119-
ticket.status = TicketStatus.AVAILABLE
120-
session.commit()
121-
122-
threads = [
123-
threading.Thread(
124-
target=book_ticket_with_lock, args=(engine, ticket_id, "Alice")
125-
),
126-
threading.Thread(target=book_ticket_with_lock, args=(engine, ticket_id, "Bob")),
127-
threading.Thread(
128-
target=book_ticket_with_lock, args=(engine, ticket_id, "Charlie")
129-
),
130-
]
131-
132-
for t in threads:
133-
t.start()
134-
for t in threads:
135-
t.join()
137+
run_scenarios(engine, ticket_id, book_ticket_with_lock)
136138

137139
print("\n" + "=" * 60)
138140
print("Scenario 3: WITH NOWAIT (Fail Fast)")
139141
print("=" * 60)
140-
141-
# Reset ticket
142-
with Session(engine) as session:
143-
ticket = session.get(Ticket, ticket_id)
144-
ticket.status = TicketStatus.AVAILABLE
145-
session.commit()
146-
147-
threads = [
148-
threading.Thread(
149-
target=book_ticket_with_lock, args=(engine, ticket_id, "Alice", True)
150-
),
151-
threading.Thread(
152-
target=book_ticket_with_lock, args=(engine, ticket_id, "Bob", True)
153-
),
154-
threading.Thread(
155-
target=book_ticket_with_lock, args=(engine, ticket_id, "Charlie", True)
156-
),
157-
]
158-
159-
for t in threads:
160-
t.start()
161-
for t in threads:
162-
t.join()
142+
run_scenarios(engine, ticket_id, book_ticket_with_lock, nowait=True)
163143

164144

165145
if __name__ == "__main__":

0 commit comments

Comments
 (0)