我有一个应用程序可以检查网站上的更改。 我需要对其进行更改,以使完成工作的第一个线程停止其余已启动的线程,并阻止创建在同一网站上工作的新线程。
我得到一些建议,我应该使用线程池,但是我不确定如何实现线程池和使用共享变量(队列)。现在,所有线程都将同一网站推送到change_queue
中,而不仅仅是发送第一个完整的结果。
下面您可以看到我到目前为止的情况
import time
from Queue import Queue
import threading
import random
change_queue = Queue()
def check_if_changed(domain, change_queue, domain_completed_event):
# Some pretend processing here
time.sleep(2)
# Request work being done here and gives back a result 1 / 0 in is_changed
is_changed = random.randint(0, 1)
if is_changed == 1:
domain_completed_event.set()
print "domain change happened for domain: " + domain
change_queue.put(domain) # Should only put each domain once for further processing (Now it can occur multiple times since other threads are already started and working on the same job)
def process_changed_domain(change_queue):
while True:
domain = change_queue.get()
print "Processing... " + domain
def main():
domain_threads = []
domains = {
'checkforchange1.com': threading.Event(),
'checkforchange2.com': threading.Event(),
'checkforchange3.com': threading.Event()
}
# Start thread that listens to the changed domains queues
t = threading.Thread(target=process_changed_domain, args=(change_queue,))
t.start()
# Go through domains
for domain in domains:
while domains[domain].is_set() == False:
t = threading.Thread(target=check_if_changed, args=(domain, change_queue, domains[domain],))
t.start()
domain_threads.append(t)
time.sleep(0.200)
for thread in domain_threads:
thread.join()
if __name__ == "__main__":
main()