title: Multi-threading in python with loop
body: <p>I'm trying to solve <a href="https://projecteuler.net/problem=8" rel="nofollow">Problem 8</a> in project euler with multi-threading technique in python. </p>
  
  <blockquote>
    <p>Find the greatest product of five consecutive digits in the 1000-digit number. The number can be found <a href="http://projecteuler.net/problem=8" rel="nofollow">here</a>.</p>
  </blockquote>
  
  <p>My approach is to generate product from chunks of 5 from the original list and repeat this process 5 times, each with the starting index shifted one to the right. </p>
  
  <p>Here is my thread class</p>
  
  
  
  <pre><code>class pThread(threading.Thread):
      def __init__(self, l):
          threading.Thread.__init__(self)
          self.l = l
          self.p = 0
  
      def run(self):
  
          def greatest_product(l):
          """
          Divide the list into chunks of 5 and find the greatest product
          """
              def product(seq):
                  return reduce(lambda x,y : x*y, seq)
  
              def chunk_product(l, n=5):
                  for i in range(0, len(l), n):
                      yield product(l[i:i+n])
  
              result = 0
              for p in chunk_product(num):
                  result = result &gt; p and result or p 
  
              return result
  
          self.p = greatest_product(self.l)
  </code></pre>
  
  <p>When I try to create 5 threads to cover all 5-digit chunks in my original list, the manual approach below gives the correct answer, with <code>num</code> being the list of single-digit numbers that I parse from the text:</p>
  
  
  
  <pre><code>thread1 = pThread(num)
  del num[0]
  thread2 = pThread(num)
  del num[0]
  thread3 = pThread(num)
  del num[0]
  thread4 = pThread(num)
  del num[0]
  thread5 = pThread(num)
  
  thread1.start()
  thread2.start()
  thread3.start()
  thread4.start()
  thread5.start()
  
  thread1.join()
  thread2.join()
  thread3.join()
  thread4.join()
  thread5.join()
  
  def max(*args):
      result = 0
      for i in args:
          result = i &gt; result and i or result
      return result
  
  print max(thread1.p, thread2.p, thread3.p, thread4.p, thread5.p)
  </code></pre>
  
  <p>But this doesn't give the correct result:</p>
  
  
  
  <pre><code>threads = []
  for i in range(0, 4):
      tmp = num[:]
      del tmp[0:i+1]
      thread = pThread(tmp)
      thread.start()
      threads.append(thread)
  
  for i in range(0, 4):
      threads[i].join()
  </code></pre>
  
  <p>What did I do wrong here? I'm very new to multithreading so please be gentle.</p>