Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

Can I execute python code on a list simultaneous instead of sequential?

First of all thank you for taking your time to read through this post. I'd like to begin that I'm very new to programming in general and that I seek advice to solve a problem.

I'm trying to create a script that checks if the content of a html page has been changed. I'm doing this to monitor certain website pages for changes. I've managed to find a script and I have made some alterations that it will go through a list of URL's checking if the page has been changed. The problem here is that its checking the page sequential. This means that it will go through the list checking the URL's one by one while I want the script to run the URL's parallel. I'm also using a while loop to continue checking the pages because even if a change took place it will still have to monitor the page. I could write a thousand more words on explaining what i'm trying to do so therefor have a look at the code:

import requests
import time
import smtplib
from email.message import EmailMessage
import hashlib
from urllib.request import urlopen


url = ["https://www.youtube.be", "https://www.google.com", "https://www.google.be"]
i = 0
response = urlopen(url[i]).read()
currentHash = hashlib.sha224(response).hexdigest()


while True:

    try:
       response = urlopen(url[i]).read()
       currentHash = hashlib.sha224(response).hexdigest()
       print('checking')
       time.sleep(10)
       response = urlopen(url[i]).read()
       newHash = hashlib.sha224(response).hexdigest()
       i +=1

       if newHash == currentHash:
          continue

       else:

          print('Change detected')
          print (url[i])
          time.sleep(10)
          continue


    except Exception as e:

          i = 0
          print('resetting increment')
          continue
question from:https://stackoverflow.com/questions/65905258/can-i-execute-python-code-on-a-list-simultaneous-instead-of-sequential

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you want to do is called multi-threading.

Conceptually this is how it works:

import hashlib
import time
from urllib.request import urlopen
import threading

# Define a function for the thread
def f(url):
    initialHash = None

    while True:
        response = urlopen(url).read()
        currentHash = hashlib.sha224(response).hexdigest()
        if not initialHash:
            initialHash = currentHash

        if currentHash != initialHash:
            print('Change detected')
            print (url)
            time.sleep(10)
            continue

    return

# Create two threads as follows
for url in ["https://www.youtube.be", "https://www.google.com", "https://www.google.be"]:
    t = threading.Thread(target=f, args=(url,))
    t.start()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...