title: How to terminate all threads at once
body: <p>My code consists of simple Flask templates with two buttons which parse titles from sites. I am facing the problem that I cannot terminate all threads at once. After pressing stop, new tasks are not created, but need to wait for completion current tasks. How to terminate all threads at once? May be multiprocessing?</p>
  <pre><code>import requests, time, threading
  from bs4 import BeautifulSoup
  from flask import Flask, render_template, request, redirect, url_for
  
  
  stop_threads = False
  
  
  def chunker_list(seq, size):
      return (seq[i::size] for i in range(size))
  
  
  def get_domens_from_file(filename):
      with open(filename, 'r') as f:
          domens = f.readlines()
          return domens
  
  
  def get_title(url):
      response = requests.get(url)
      soup = BeautifulSoup(response.text, 'html.parser')
      title = soup.find('title').text.replace('\n', '').replace('  ', '')
      return title
  
  
  def start_work(domens):
      for domen in domens:
          global stop_threads
          if stop_threads:
              return 0
          title = get_title(domen.strip())
          print(title)
          time.sleep(1)
  
  
  
  app = Flask(__name__)
  
  
  @app.route('/', methods=['POST', 'GET'])
  def main():
      global stop_threads
  
      if request.method == 'POST':
          index = request.form['index']
          if index == 'start':
  
              stop_threads = False
  
              domens = get_domens_from_file('C:\\Users\\Admin\\Desktop\\domains.txt')
              domens_list = list(chunker_list(domens, 5))
  
              for domens in domens_list:
                  threading.Thread(target = start_work, args=(domens, )).start()
  
          if index == 'stop':
              stop_threads = True
              print('stop')
  
          return redirect(url_for('main'))
  
      return render_template('index.html')
  
  app.run(debug=True)
  </code></pre>