title: How to avoid redundancy when using threads?
body: <p>I have a big txt file in which there are strings. I want to apply several functions on these strings with the shortest time. I then thought of using threads.
  Let's say I have these two functions:</p>
  
  <pre class="lang-py prettyprint-override"><code>def length(string):
      print len(string)
  
  def numberOfPoints(string):
      print string.count('.')
  </code></pre>
  
  <p>Without threads I would do:</p>
  
  <pre class="lang-py prettyprint-override"><code>with open('bigfile.txt', 'r') as f:
      samples = f.read().splitlines()
  
  for s in samples:
      length(s)
      numberOfPoints(s)
  </code></pre>
  
  <p>My problem occurs when I want to use threads, I would do in this way:</p>
  
  <pre class="lang-py prettyprint-override"><code>import threading
  
  with open('bigfile.txt', 'r') as f:
      samples = f.read().splitlines()
  
  for s in samples:
      thread1 = threading.Thread(target=length, args=(s,))
      thread2 = threading.Thread(target=numberOfPoints, args=(s,))
      thread1.start()
      thread2.start()
      thread1.join()
      thread2.join()
  </code></pre>
  
  <p>But in doing so, I feel like I am missing the inner property threads provide. I want a thread that computes the length of each string in the file and an other that computes the number of points of each string in the file. In my loop, I am creating (2 * the number of strings in my file) threads. How can I load all the strings from the file without reading the file twice like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>import threading
  
  def length(samples):
      for s in samples:
          print len(s)
  
  def numberOfPoints(samples):
      for s in samples:
          print s.count('.')
  
  with open('bigfile.txt', 'r') as f:
      samples = f.read().splitlines()
  
  thread1 = threading.Thread(target=length, args=(samples,))
  thread2 = threading.Thread(target=numberOfPoints, args=(samples,))
  thread1.start()
  thread2.start()
  thread1.join()
  thread2.join()  
  </code></pre>