title: What is the safest way to queue multiple threads originating in a loop?
body: <p>My script loops through each line of an input file and performs some actions using the string in each line. Since the tasks performed on each line are independent of each other, I decided to separate the task into <code>threads</code> so that the script doesn't have to wait for the task to complete to continue with the loop. The code is given below.</p>
  <pre><code>
  def myFunction(line, param):
      # Doing something with line and param
      # Sends multiple HTTP requests and parse the response and produce outputs
      # Returns nothing
  
  param = arg[1]   
  with open(targets, &quot;r&quot;) as listfile:
      for line in listfile:
          print(&quot;Starting a thread for: &quot;,line)
          t=threading.Thread(target=myFunction, args=(line, param,)) 
          threads.append(t)
          t.start()
  
  </code></pre>
  <p>I realized that this is a bad idea as the number of lines in the input file grew large. With this code, there would be as many threads as the number of lines. Researched a bit and figured that <code>queues</code> would be the way.</p>
  <p>I want to understand the optimal way of using queues for this scenario and if there are any alternatives which I can use.</p>