Reputation: 2227
Last attempt to get an answer.
I need to run 4 functions in an infinite loop on different threads.
The error is that only the first thread runs. Feel free to ask why the functions are like what they are.
import time
import threading
import requests
from bs4 import BeautifulSoup
headers = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'}
def carrigaline(): #Get the first Ad's link
while True:
try:
page = requests.get(
'https://www.daft.ie/property-for-sale/carrigaline-and-surrounds-cork?salePrice_from=200000&salePrice_to=300000',
headers=headers).text
soup = BeautifulSoup(page, 'html.parser')
divs = soup.findAll('a', attrs={'class': None})
cur_carri = 'https://www.daft.ie' + divs[0].get('href')
return cur_carri
except Exception as e:
print(e)
pass
def crosshaven():
while True:
try:
page = requests.get(
'https://www.daft.ie/property-for-sale/crosshaven-cork?salePrice_from=200000&salePrice_to=300000',
headers=headers).text
soup = BeautifulSoup(page, 'html.parser')
divs = soup.findAll('a', attrs={'class': None})
cur_haven = 'https://www.daft.ie' + divs[0].get('href')
return cur_haven
except Exception as e:
print(e)
pass
def minane():
while True:
try:
page = requests.get(
'https://www.daft.ie/property-for-sale/minane-bridge-cork?salePrice_from=200000&salePrice_to=300000',
headers=headers).text
soup = BeautifulSoup(page, 'html.parser')
divs = soup.findAll('a', attrs={'class': None})
cur_min = 'https://www.daft.ie' + divs[0].get('href')
return cur_min
except Exception as e:
print(e)
pass
def ballygarvan():
while True:
try:
page = requests.get(
'https://www.daft.ie/property-for-sale/ballygarvan-cork?salePrice_from=200000&salePrice_to=300000',
headers=headers).text
soup = BeautifulSoup(page, 'html.parser')
divs = soup.findAll('a', attrs={'class': None})
cur_bally = 'https://www.daft.ie' + divs[0].get('href')
return cur_bally
except Exception as e:
print(e)
pass
first_run = True
while True:
print("Starting 1st thread")
time.sleep(6) #Sleep 5 minutes
bally = ballygarvan()
first_run = False
new_bally = ballygarvan()
print(new_bally)
if not first_run:
if new_bally != bally:
print("New Ad")
continue
while True:
print("starting second")
time.sleep(5) #Sleep 5 minutes
min_a = minane()
first_run = False
new_mina = minane()
print(new_mina)
if not first_run:
if new_mina != min_a:
print("New Ad")
continue
while True:
print("starting third")
time.sleep(7) #Sleep 5 minutes
carr = carrigaline()
first_run = False
new_carr = carrigaline()
print(new_carr)
if not first_run:
if new_carr != carr:
print("New Ad")
continue
while True:
print("starting fourth")
time.sleep(9) #Sleep 5 minutes
cross = crosshaven()
first_run = False
new_cross = crosshaven()
print((new_cross))
if not first_run:
if new_cross != cross:
print("New Ad")
continue
if __name__ == "__main__":
t1 = threading.Thread(target=carrigaline, name='t1')
t1.start()
t2 = threading.Thread(target=minane, name='t2')
t2.start()
t3 = threading.Thread(target=crosshaven, name='t3')
t3.start()
t4 = threading.Thread(target=ballygarvan, name='t4')
t4.start()
Upvotes: 0
Views: 40
Reputation: 2129
I think you expect your code to start at
if __name__ == "__main__":
t1 = threading.Thread(target=carrigaline, name='t1')
But it actually enters this while loop first :
first_run = True
while True:
print("Starting 1st thread")
time.sleep(6) #Sleep 5 minutes
bally = ballygarvan()
first_run = False
new_bally = ballygarvan()
print(new_bally)
if not first_run:
if new_bally != bally:
print("New Ad")
continue
and since it is an infinite loop the code is stuck there.
This happens because the loop is not inside any function. So main function is never called and your threads are not started.
Upvotes: 1