title: Write data to multiple files using multiple threads
body: <p>I want to create multiple file using multiple threads, and append data (after some operation is performed) to corresponding files from their respective threads.</p>
  
  <p>I tried it, but data is getting messed between threads and correct data is not added to respective files.</p>
  
  <pre><code>import threading
  import time
  
  exitFlag = 0
  
  class myThread (threading.Thread):
     def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
     def run(self):
        with open('file_'+count+'_logs.txt', 'a+') as result:
              result.write("Starting " + self.name)
              result.write("Exiting " + self.name)
        print ("Starting " + self.name)
        print_time(self.name, self.counter, 5)
        print ("Exiting " + self.name)
  
  def print_time(threadName, delay, counter):
     while counter:
        if exitFlag:
           threadName.exit()
        time.sleep(delay)
        print ("%s: %s" % (threadName, time.ctime(time.time())))
        counter -= 1
  
  myList = ['string0', 'string1', 'string2', 'string3']
  
  if __name__ == "__main__":
      count = 0
      for data in myList:
          count += 1
          mythread = myThread(count, "Thread-" + str(count), count)
          mythread.start()
          mythread.join()
  
  </code></pre>
  
  <p>I expect that 4 files shall be created from 4 threads, and data from thread 1 should be written to file_1_logs.txt and so on...
  But while writing data, sometimes all data is written in a single file.
  How do I write this data to file correctly?</p>