Chapter 12. 11¾ A Little Bit of Threading: Dealing with Waiting

image with no caption

Your code can sometimes take a long time to execute. Depending on who notices, this may or may not be an issue. If some code takes 30 seconds to do its thing “behind the scenes,” the wait may not be an issue. However, if your user is waiting for your application to respond, and it takes 30 seconds, everyone notices. What you should do to fix this problem depends on what you’re trying to do (and who’s doing the waiting). In this short chapter, we’ll briefly discuss some options, then look at one solution to the issue at hand: what happens if something takes too long?

Waiting: What to Do?

When you write code that has the potential to make your users wait, you need to think carefully about what it is you are trying to do. Let’s consider some points of view.

image with no caption

Maybe it is the case that waiting for a write is different from waiting for a read, especially as it relates to how your webapp works?

Let’s take another look at the SQL queries in log_request and view_the_log to see how you’re using them.

How Are You Querying Your Database?

In the log_request function, we are using an SQL INSERT to add details of the request to our backend database. When log_request is called, it waits while the INSERT is executed by cursor.execute:

Geek Bits ...

Get Head First Python, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.