the solution to this problem in threaded programming is to "synchronizeaccess to any code that reads or writes shared variable there are few different ways to do thisbut we won' go into them here so we can focus on more pythonic constructs the synchronization solution worksbut it is way too easy to forget to apply it worsebugs due to inappropriate use of synchronization are really hard to track down because the order in which threads perform operations is inconsistent we can' easily reproduce the error usuallyit is safest to force communication between threads to happen using lightweight data structure that already uses locks appropriately python offers the queue queue class to do thisit' functionality is basically the same as the multiprocessing queue that we will discuss in the next section in some casesthese disadvantages might be outweighed by the one advantage of allowing shared memoryit' fast if multiple threads need access to huge data structureshared memory can provide that access quickly howeverthis advantage is usually nullified by the fact thatin pythonit is impossible for two threads running on different cpu cores to be performing calculations at exactly the same time this brings us to our second problem with threads the global interpreter lock in order to efficiently manage memorygarbage collectionand calls to machine code in librariespython has utility called the global interpreter lockor gil it' impossible to turn offand it means that threads are useless in python for one thing that they excel at in other languagesparallel processing the gil' primary effectfor our purposes is to prevent any two threads from doing work at the exact same timeeven if they have work to do in this case"doing workmeans using the cpuso it' perfectly ok for multiple threads to access the disk or networkthe gil is released as soon as the thread starts to wait for something the gil is quite highly disparagedmostly by people who don' understand what it is or all the benefits it brings to python it would definitely be nice if our language didn' have this restrictionbut the python reference developers have determined thatfor now at leastit brings more value than it costs it makes the reference implementation easier to maintain and developand during the single-core processor days when python was originally developedit actually made the interpreter faster the net result of the gilhoweveris that it limits the benefits that threads bring uswithout alleviating the costs