Ctrl + c не работает с очередью + Многопоточность python? - PullRequest
0 голосов
/ 29 апреля 2020

Я пытаюсь поймать CTRL+c, как я могу остановить этот код, когда он работает. Вот код

from threading import Thread
from queue import Queue
import requests

q = Queue(maxsize=0) # Create Queue 
fileIP = 'path.txt'
for i in fileIP: # put all lines in queue 
    q.put(i)

Path_vlunIPs = '/path.txt'
counterFound = 0

Функция: -1

def checkFolders(urlIP):   # I'will call this function with workerss function 
    global Path_vlunIPs
    global counterFound
    tt = 'path.txt'
    with open(tt, "r", errors='ignore', encoding="utf-8") as f:
        for i in f:
            try:
                url = urlIP + \
                    "{}/".format(i.strip())
                istek = requests.get(
                    url, verify=False)  # proxies=proxyy
                if istek.status_code == 200 :
                    # do something and break 
                    break  # here I want to break the for loop and kill the thread that work done its work in this function
                else:
                    print("[+] Not found :" + str(url)

            except requests.exceptions.MissingSchema as s:
                print('You have to filter ur ip\'s file')
                break
            except :
                pass

Отключите функцию workerss и сгенерируйте поток с помощью для l oop

def workerss(): # here I get items from Queue to pass them to checkFolders() function 
    try:
        while True:
            try:
                if q.empty() == True:
                    sys.exit(1)
                ip = q.get(True, 1)
                checkFolders(ip)  # here we go 
                q.task_done()
            except Exception as e:
                print(e)
                print('queqe area ')
                pass
    except Exception as e:
        print(e)

for i in range(maxThread):
    worker = Thread(target=workerss)
    worker.setDaemon(True)
    worker.start()
q.join()

""" 
  aslo after finish the first step of the code there are some codes down to complete my program 

"""

Также, если у вас есть идея ускорить выполнение кода, я благодарен за вас. Заранее спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...