0

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?

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)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Dmitry
  • 15
  • 4
  • Do you want to terminate *just* the threads or do you actually want to terminate the entire application? – MisterMiyagi Nov 06 '21 at 12:58
  • Only all threads at once by pressing the stop button – Dmitry Nov 06 '21 at 13:00
  • User traces to kill. More info [here](https://www.geeksforgeeks.org/python-different-ways-to-kill-a-thread/) – Irfanuddin Nov 06 '21 at 13:12
  • can you show an example on my code? – Dmitry Nov 06 '21 at 13:14
  • @MisterMiyagi, What does "just the threads" mean? If you terminate all of the threads in an application, that's the same thing as terminating the application. Threads are what execute the application's code. If there are no threads, no code can be executed. – Solomon Slow Nov 06 '21 at 14:57
  • @SolomonSlow Most people requesting to terminate threads mean just a few they have explicitly started. Not the main thread, not auxiliary threads launched by other routines. So "just the threads you have started explicitly" is what I was aiming at. – MisterMiyagi Nov 06 '21 at 16:09

0 Answers0