线程之间的通信:
1)通过queue的方法来实现线程通信
from queue import Queue
def get_html_detail(queue):
while True:
url = queue.get()
# url = queue.get_nowait()
print('开始获取界面{url}'.format(url=url))
time.sleep(3)
print('获取界面{url}成功---------------------------------------------------------------------------------'.format(url=url))
def get_url(queue):
while True:
print('获取界面元素url开始')
time.sleep(0.5)
for i in range(5):
queue.put("http://projecedu.com/{id}".format(id=i))
print('获取界面元素url结束')
if __name__ == '__main__':
url_list_queue = Queue(1000)
thread_get_url = threading.Thread(target=get_url,args=(url_list_queue,))
thread_get_url.start()
for i in range(10):
html_thread = threading.Thread(target=get_html_detail,args=(url_list_queue,))
html_thread.start()
start_time = time.time()
time.sleep(1)
#queue发送一个task_down
url_list_queue.task_done()
#queue的join函数当接收到一个task_done信号时阻塞主进程。
url_list_queue.join()
print('主线程用时{}'.format(time.time() - start_time))