赞
踩
# -*- coding:utf-8 -*- import threading,queue,time base_url = "http://192.168.1." url_queue = queue.Queue() for i in range(1,31): url = base_url+str(i) url_queue.put(url) def fetUrl(url_queue): while url_queue.qsize()>0: try: # 不阻塞的读取队列数据 url = url_queue.get_nowait() i = url_queue.qsize() except Exception as e: print(e) break print('Current Thread Name %s, Url: %s ' % (threading.currentThread().name, url)) if __name__ == '__main__': startTime = time.time() threads = [] threadNum = 4 for i in range(0,threadNum): try: t = threading.Thread(target=fetUrl,args=(url_queue,)) threads.append(t) except: print("加入线程失败") break for t in threads: try: t.start() except: print('开启线程失败') break for t in threads: try: t.join() except: print('启动线程失败') break endTime = time.time() print('Done, Time cost: %s ' % (endTime - startTime))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。