Using threads to fetch from the IPmirrors

This commit is contained in:
theo@manjaro 2022-07-04 10:59:17 +02:00
parent 53941d7657
commit f6695eb9c4

View file

@ -22,6 +22,7 @@ import os
import re
import logging
import time
import threading
from moulinette.utils.filesystem import read_file, write_to_file
from moulinette.utils.network import download_text
@ -102,8 +103,7 @@ def get_public_ips(protocol=4):
ip_url_yunohost_tab = settings_get("security.ipmirrors.v"+str(protocol)).split(",")
ip_count = {} # Count the number of times an IP has appeared
# Check URLS
for url in ip_url_yunohost_tab[:3]:
def thread_fun(ip_count,url):
logger.debug(f"Fetching IP from {url}")
try:
ip = download_text(url, timeout=15).strip()
@ -116,6 +116,15 @@ def get_public_ips(protocol=4):
f"Could not get public IPv{protocol} from {url} : {e}"
)
# Check URLS
threads = []
for url in ip_url_yunohost_tab[:3]: # Launch threads
thread = threading.Thread(target=thread_fun, args=(ip_count,url))
thread.start()
threads.append(thread)
for thread in threads: # Wait for the threads to finish
thread.join()
ip_list_with_count = [ (ip,ip_count[ip]) for ip in ip_count ]
ip_list_with_count.sort(key=lambda x: x[1]) # Sort by frequency
ips = [ x[0] for x in ip_list_with_count ]