from multiprocessing.dummy import Pool as ThreadPool
import requests
import random
import time
def func_main(thr):
time1 = time.time()
while True:
proxy_line = proxy_lines[random.randint(1, len(proxy_lines)-1)].strip()
session = requests.session()
session.temeout = (5 , 5)
Adapter = requests.adapters.HTTPAdapter(pool_connections = 1, pool_maxsize = 0, max_retries = 0)
session.mount('http://', Adapter)
session.mount('https://', Adapter)
session.proxies = {
'http' : proxy_line,
'https' : proxy_line
}
try:
request = session.post('http://www.yahex.ru/')
request.close()
break
except Exception as exception:
#print(exception)
#print('Thread #' + str(thr + 1) + '|Reconnect|')
pass
time2 = time.time() - time1
global thr_quantity
thr_quantity = thr_quantity - 1
print('Thread #' + str(thr + 1) + ' завершил работу|Прокси:' + proxy_line + '|Активных потоков:' + str(thr_quantity) + '|Время выполнения:{:.3f}'.format(time2))
return()
#*******************************************************************#
with open('C:\\proxy.txt' , 'r', encoding = 'utf-8') as proxy_file:
proxy_lines = proxy_file.readlines()
class Profiler(object):
def __enter__(self):
self._startTime = time.time()
def __exit__(self, type, value, traceback):
print("*************************\nВремя выполнения программы : {:.3f} sec".format(time.time() - self._startTime))
thr_quantity = 50
with Profiler() as p:
with ThreadPool(thr_quantity) as pool:
pool.map(func_main, range(thr_quantity))
pool.close()
Проблема в следующем: запускаю код выше в 50 потоков, к примеру, 49 потоков выполняются, а на 50-ом потоке всё зависает и поток не выполняется. Либо на последнем виснет, либо на последних 10-ти потоках. Примерно так. Не могу понять в чем причина.
На скриншоте ниже показана скорость выполнения потоков. Последний аж 10 минут чем то занимался. Но в большинстве случаев он не завершается.
session.temeout
наsession.timeout
заменить?