Sleep
function doesn't pause execution of script for given random time (sleep(random.uniform(2, 3))) inside while loop. Weird thing is that it's working ok in for
loop.
I'm running scripts on Ubuntu 20.04, Python 3.8.5
I've added flush=True
as suggested here but no luck. Also sleep(random.uniform(2, 3))
works ok on it's own but not in while
loop.
Here is my script:
import os
import random
from time import sleep
proxyFilePath = "data/proxies/httpsproxies.txt"
print("Get list of proxies from: ", proxyFilePath)
proxy = []
sleep(random.uniform(2, 3))
i=0
while len(proxy) < 10:
i += 1
print("#### TRY #### ", i)
print(len(proxy))
try:
if os.path.getsize(proxyFilePath) > 0:
print("file size", os.path.getsize(proxyFilePath))
with open(proxyFilePath, "r") as f:
read_lines = f.readlines()
proxy = [line.rstrip('\n') for line in read_lines]
sleep(random.uniform(2, 3))
print("proxy size", len(proxy), end='', flush=True)
else:
sleep(random.uniform(2, 3))
print("proxy file is empty - try again", end='', flush=True)
except OSError as e:
print("File does not exists or is non accessible")
Here is sample entries of httpsproxies.txt
:
109.185.243.41:8081
101.255.103.201:53281
103.94.199.41:8080
110.74.195.65:55443
103.231.218.126:53281
103.86.47.243:8888
118.179.173.253:40836
111.118.128.140:41344
136.226.33.115:80
130.61.102.41:80
165.22.185.146:3128
167.172.180.40:41621
I have another process which is filling this file with entries. I can confirm that reading this file content to python list works correctly.
Aucun commentaire:
Enregistrer un commentaire