1

I have a list of strings, which is used for processing some data. Data processing for all strings does not affect result of other string..

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):
      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']

def processFunc():
    count = 0
    for data in myList:
        count += 1
        mythread = myThread(count, "Thread-" + str(count), count)
        mythread.start()
        mythread.join()

processFunc()

This is executing in proper sequence, and not simultaneously. How can I implement it using threads, so that all data is processed simultaneously?

1 Answer 1

2

join() waits for the thread to finish , so you have to call later after starting all thread.

def processFunc():
    count = 0
    mythreads=[]
    for data in myList:
        count += 1
        mythread = myThread(count, "Thread-" + str(count), count)
        mythread.start()
        mythreads.append(mythread)
    for mythread in mythreads:
        mythread.join()

processFunc()
Sign up to request clarification or add additional context in comments.

2 Comments

If i don't use .join(), then will there be any complication in the execution?
Nope, unless you need all processed data for further process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.