What are some examples of thread-unsafe Python code?
Question: What are some examples of Python code that is not safe to run in a threaded
environment? Examples need not be framework-related -- simple Python examples
and caveats are preferred. Thanks!


Answer: The following function may write to the file / overwrite the file in an
unpredictable way.

    
    
     import threading
    
     fp = open('f','w')
    
     def work1():
         for x in range(10000):
             fp.write('1')
     def work2():
         for x in range(10000):
             fp.write('2')
    
     t1 = threading.Thread(target = work1)
     t1.daemon = True
     t2 = threading.Thread(target = work2)
     t2.daemon = True
    
     t1.start()
     t2.start()
     t1.join()
     t2.join()
    

On the other hand, the locking mechanism here will prevent the file output
from getting mixed up.

    
    
     import threading
     lock = threading.Lock()
    
     fp = open('f','w')
    
     def work1():
         with lock:
             for x in range(10000):
                 fp.write('1')
     def work2():
         with lock:
             for x in range(10000):
                 fp.write('2')
    
     t1 = threading.Thread(target = work1)
     t1.daemon = True
     t2 = threading.Thread(target = work2)
     t2.daemon = True
    
     t1.start()
     t2.start()
     t1.join()
     t2.join()
    

