title: python file writing in threads not writing all lines
body: <p>I tested my code output and sort it, but I am not having 0-1999 as expected, lines is missing. Is my code thread unsafe? please suggest how to add thread lock in my code, and i found that my except all doesn't throw any errors, is it correct? thanks</p>
  
  <pre><code>import time, threading, random
  
  class MyThreadWithArgs(threading.Thread):
      def __init__(self, string):
          threading.Thread.__init__(self)
          self.string = string
  
      def run(self):
          try:
              fo = open("foo.np2", "a")
              fo.write( self.string )
              fo.write( '\n' )
              time.sleep(random.uniform(0.1, 0.9))
              fo.close()
          except:
              print ("error logging " + self.string)
  
  ttl_threads = 2000
  for i in range(ttl_threads):
      t = MyThreadWithArgs(string = str(i))
      t.start()
  </code></pre>