简体   繁体   中英

How to get simple threading to work Python

i want to know how i can add simple threading to my code. At the moment it checks just one by one, and if some site isnt reachable it will wait for the timeout before it will continue with the next one this slows everything down.

import requests
import sys
import time
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


with open("websites.txt", 'r') as websites:
    websites = websites.read().splitlines()

with open("para1.txt", 'r') as para1:
    para1 = para1.read().splitlines()

with open("para2.txt", 'r') as para2:
   para2 = para2.read().splitlines()

def main():
    for i in para1:
        for j in para2:
            for m in websites:
                try:
                    res = requests.get(m + i + j, verify=False, timeout=10)
                    print(m + i + j)
                    if res.status_code == 200:
                        print('Yes')
                    else:
                        print('No')
                except Exception as e:
                    print(e)
                except KeyboardInterrupt:
                    sys.exit()
                finally:
                    res.close()
                    time.sleep(1)

if __name__ == '__main__':
    main()

You can apply ThreadPoolExecutor moving part of code which perform requests to separate function and pass it as argument:

import urllib3
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def check_func(url):
    response = requests.get(url, verify=False, timeout=10)
    return response.status_code == 200
    
def main():
    with open("websites.txt") as website_f, open("para1.txt") as para1_f,
        open("para2.txt", 'r') as para2_f, ThreadPoolExecutor(max_workers=4) as executor:
        tasks = {}
        for website in website_f:
            for para1 in para1_f:
                for para2 in para2_f:
                    url = website.rstrip() + para1.rstrip() + para2.rstrip()
                    tasks[executor.submit(check_func, url)] = url

        for task in as_completed(tasks):
            url = tasks[task]
            try:
                result = task.result()
            except KeyboardInterrupt:  # handling Ctrl + C
                for task in tasks:
                    task.cancel()      # won't cancel already finished or pending futures
            except CancelledError:     # will never happen (normally)
                pass                            
            except Exception as e:
                print(url, "-", "ERROR", e)
            else:
                print(url, "-", "GOOD" if result else "BAD")
        
if __name__ == "__main__":
    main()

PS I haven't tested entire code so if there're any problems with it - write in comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM