Every book in the shelf has a small download button under its cover. Tap it and the badge changes to a spinning arrow, then to a checkmark that reads "already parsed." Nothing about the book has changed. Nobody is reading it yet. The work that used to happen when you opened it has just happened earlier, while you were still looking at the shelf.
The first open is the slow one
Opening a book is not opening a file. An EPUB, MOBI, or TXT file has to be parsed into something the reader can page through — chapters found, text extracted, a reader object built. The first time you open a book, you wait for all of that. The second time, you don't, because the parsed result is sitting in the reader cache. So the slow open is really a first-parse problem, and first parses are the only ones that have to hurt.
That framing matters because it rules out most of the obvious fixes. You could parse faster, and people do work on that. But you can't parse a book before it exists on the shelf, and you can't make the first parse free. What you can do is move it. The shelf already knows which books are there. The question is only when the parse runs.
The same parse, run earlier
The background job does not have its own parser. When a preload worker picks up a book, it calls the same get_reader the open path calls, and the provider writes the parsed reader into the same reader cache the open path reads from. There is no second copy of the parsing logic and no second cache to keep in sync. A preloaded book opens fast for exactly the same reason a reopened book does: the reader object is already there.
This is also why preload can be wrong in such a boring way. If the worker crashes, there is no half-written cache to clean up — the parse either landed in the cache or it didn't. The worker just drops its "loading" marker and records the failure. And if you open the book yourself while a preload is running, the open path parses it the normal way and the cache ends up filled either way. Two routes into the same cache means neither one has to know about the other.
Two threads, and a button that means it
The pool has two worker threads. Two, not four — the module docstring still says four, but the constant in the code is two, and two is what runs. That number is the whole argument about how aggressive to be. Two threads can warm books in the background without crowding out the request that is actually opening a book right now. A bigger pool would finish the queue sooner and make everything else slower while it did. The shelf is not a batch system; somebody is usually in the middle of reading.
The trigger is deliberate, too. Nothing preloads on its own. Each book card has its own button, and there is a "preload all" button for the page you are looking at. No scheduler walks the library at night, no startup pass warms everything. That restraint is doing real work: preloading is a bet that you will open this book soon, and the bet is only worth placing when you say so. A book you never open is parse time spent for nothing, and on a small server that time comes out of somebody else's open.
There are guards before the queue as well. A book with no file behind it gets refused instead of queued to fail. An oversized file gets refused instead of handed to a worker. A user over their parse quota gets a 429 instead of a job. Each of these is a decision not to spend a thread on work that was never going to succeed.
Four words for where a book stands
The shelf shows one of four states for each book: idle, loading, ready, error. That is the entire interface between the background job and the person waiting on it, and four words turn out to be enough because each one answers a different question. Idle means nothing has been asked for. Loading means the worker has it. Ready means the cache has it. Error means something went wrong and you can try again.
The frontend polls for these states every five seconds, in one batch request for the whole page rather than one request per book. The polling is plain and a little crude, but it fits the job: preloads take a while, the shelf can afford to check in every few seconds, and a batch of up to two hundred ids keeps the chatter down.
What makes the four states hold together is that only one of them is stored. Loading lives in a dictionary in the server process, and it vanishes when the process restarts. Ready is never stored at all — it is checked, each time, by looking for the reader cache under the book's key. Error is a set of ids kept in the same process. So after a restart, the server forgets what was loading and what failed, but it does not forget what is ready, because ready was never a claim the server made. It was a fact about the cache.
Why ready is checked, not remembered
That choice pays off in one specific case. Suppose a preload fails and the book sits in error. Then you open the book anyway, the normal way, and the open path parses it and fills the cache. The next status check looks at the cache first, sees the reader object, clears the stale error, and reports ready. The book does not care which route filled its cache. The status just describes what is true now.
There is a similar fallback for PDFs. If the cache was cleared by hand but the high-quality parse result is still sitting on disk as a file, the check counts the book as ready. The cache check is doing the honest thing: asking whether the parsed result exists, in whichever place it could exist, rather than trusting a flag that says it should.
None of this makes opening a book instant. It moves the waiting to a moment you chose, bounds it to two threads, and shows you plainly where each book stands. The shelf does not pretend the parse went away. It just lets you get it done before you need it.
About the author
- Writing about ebook technology.
Comments
Join the discussionNo comments yet. Be the first to comment.