Python. Multi-procressing



最后一次尝试获取答案。

我需要在不同线程的无限循环中运行4个函数。

误差只有第一个线程运行。你可以随意问为什么这些函数是这样的

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()

我想你希望你的代码从

开始
if __name__ == "__main__":
t1 = threading.Thread(target=carrigaline, name='t1')

但是它实际上先进入这个while循环:

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

,因为它是一个无限循环,所以代码被卡在那里。

这是因为循环不在任何函数内部。所以main函数永远不会被调用,线程也不会启动。

最新更新