title: Is this Python code a safe way to use multi-threading
body: <p>An application I use for graphics has an embedded Python interpreter  -  It works exactly the same as any other Python interpreter except there are a few special objects.</p>
  
  <p>Basically I am trying to use Python to download a bunch of images and make other Network and disk I/O. If I do this without multithreading, my application will freeze (i.e. videos quit playing) until the downloads are finished.  </p>
  
  <p>To get around this I am trying to use multi-threading. However, I can not touch any of the main process.</p>
  
  <p>I have written this code. The only parts unique to the program are commented. <code>me.store</code> / <code>me.fetch</code> is basically a way of getting a global variable. <code>op('files')</code> refers to a global table.</p>
  
  <p>These are two things, "in the main process" that can only be touched in a thread safe way. I am not sure if my code does this.</p>
  
  <p>I would apprecaite any input as to why or (why not) this code is thread-safe and how I can get around access the global variables in a thread safe way.</p>
  
  <p>One thing I am worried about is how the <code>counter</code> is fetched multiple times by many threads.  Since it is only updated after the file is written, could this cause a race-condition where the different threads access the counter with the same value (and then don't store the incremented value correctly). Or, what happens to the counter if the disk write fails.</p>
  
  <pre><code>from urllib import request
  import threading, queue, os
  
  url = 'http://users.dialogfeed.com/en/snippet/dialogfeed-social-wall-twitter-instagram.json?api_key=ac77f8f99310758c70ee9f7a89529023'
  
  imgs = [
      'http://search.it.online.fr/jpgs/placeholder-hollywood.jpg.jpg',
      'http://www.lpkfusa.com/Images/placeholder.jpg',
      'http://bi1x.caltech.edu/2015/_images/embryogenesis_placeholder.jpg'
  ]
  
  def get_pic(url):
      # Fetch image data
      data = request.urlopen(url).read()
      # This is the part I am concerned about, what if multiple threads fetch the counter before it is updated below
      # What happens if the file write fails?
      counter = me.fetch('count', 0)
  
      # Download the file
      with open(str(counter) + '.jpg', 'wb') as outfile:
          outfile.write(data)
          file_name = 'file_' + str(counter)
          path = os.getcwd() + '\\' + str(counter) + '.jpg'
          me.store('count', counter + 1)
          return file_name, path
  
  
  def get_url(q, results):
      url = q.get_nowait()
      file_name, path = get_pic(url)
      results.append([file_name, path])
      q.task_done()
  
  def fetch():
      # Clear the table
      op('files').clear()
      results = []
      url_q = queue.Queue()
      # Simulate getting a JSON feed
      print(request.urlopen(url).read().decode('utf-8'))
  
      for img in imgs:
          # Add url to queue and start a thread
          url_q.put(img)
          t = threading.Thread(target=get_url, args=(url_q, results,))
          t.start()
  
      # Wait for threads to finish before updating table
      url_q.join()
      for cell in results:
          op('files').appendRow(cell)
      return
  
  # Start a thread so that the first http get doesn't block
  thread = threading.Thread(target=fetch) 
  thread.start()
  </code></pre>