Page 346 -
P. 346

avoid race conditions


          You need a better data storage mechanism


          Your initial text files and pickle design is fine when only one user is accessing
           the data. However, now that more than one person can access the data at any
           time, and from anywhere, your design is in need of improvement. Above all,
           you need to avoid that race condition.




                     This is very                               Listen, bud, it’s not my fault...until
                    upsetting...I appear                someone, somewhere runs the “put_to_store()”
                   to be missing an update.           function without someone, somewhere else running the
                                                      “get_from_store()” function, you’ll have to do without that
                                                      data update. I’m not a miracle worker...I just do what
                                                               I’m told.





                   [‘Is this the right room
                   for an argument?’, “No
                   you haven’t!”, ‘When?’,
                   “No you didn’t!”, “You
                   didn’t!”, ‘You did not!’,
                   ‘Ah! (taking out his wallet
                   and paying) Just the five
                   minutes.’, ‘You most
                   certainly did not!’, “Oh
                   no you didn’t!”, “Oh no
                   you didn’t!”, “Oh look,
                   this isn’t an argument!”,
                   “No it isn’t!”, “It’s
                   just contradiction!”,
                   ‘It IS!’, ‘You just
                   contradicted me!’, ‘You
                   DID!’, ‘You did just
                   then!’, ‘(exasperated)
                   Oh, this is futile!!’,
                   ‘Yes it is!’]
                          Your inconsistent
                          and upset pickle file                        Server
                                                                       Web
           Q:   Surely you should have thought about this problem long   Q:  So I’m facing a rewrite of large chunks of my code?
           ago and designed this “properly” from the start?
                                                            A: Let’s see. You did build your program using the MVC pattern,
          A: That’s certainly one way to look at things, and hindsight is   and you are using Python, so those two facts should take the sting
           always a wonderful thing! However, programs have a tendency   out of any potential rewrite, assuming a rewrite is what’s required
           to start out small, then grow to provide more features, which can   here.
           introduce complexity. Recall that the coach’s app started life as a
           simple “standalone” text-based program, which was then moved
           to the Web to support multiple users. Part of the app was then
           redeveloped for use on an Android phone. And yes, if we’d known all
           of this ahead of time, we might have been designed it differently.


           310    Chapter 9
   341   342   343   344   345   346   347   348   349   350   351