Page 345 -
P. 345

manage your data


           Avoid race conditions







                                            Of course...I could
                                           write to the text file
                                           and then immediately call
                                          “put_to_store()” to update
                                            the pickle, right?





                                          Yes, that’s one possible solution, but it’s a poor one.
                                          You might think it highly unlikely…but it is possible for another
                                          process to call the get_from_store() function between the text file
                                          update and the pickle recreation, resulting in a short period of data
                                          inconsistency. These types of situations are known as race conditions
                                          and are hard to debug when they occur.
                                          It’s best to keep them from ever happening if you can.
                                          The basic problem here is that you have one update with one piece of data
                                          that results in two file interactions. If nothing else, that’s just wasteful.








                                 Hey, thanks for                             What update?!?
                                 the update!                                 It’s been all quiet
                                                                             over here...





                                                                [‘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!!’,
               Your up-to-                                      ‘Yes it is!’]
               date text file                                         Your temporarily
                                                                       inconsistent pickle file
                                                                                      you are here 4    309
   340   341   342   343   344   345   346   347   348   349   350