# -*- coding:utf-8 -*-
""" Created by FizLin on 2017/07/23/-下午10:59
mail: https://github.com/Fiz1994
信号量
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
Once spawned, worker threads call the semaphore's acquire and release methods when they need to connect to the server:
pool_sema.acquire()
conn = connectdb()
... use connection ...
conn.close()
pool_sema.release()
"""
import threading
import time
import random
sites = ["https://www.baidu.com/", "https://github.com/Fiz1994", "https://stackoverflow.com/",
"https://www.sogou.com/",
"http://english.sogou.com/?b_o_e=1&ie=utf8&fr=common_index_nav&query="] * 20
sites_index = 0
maxconnections = 2
pool_sema = threading.BoundedSemaphore(value=maxconnections)
def test():
with pool_sema:
global sites_index, sites
url = str(sites[sites_index])
k = random.randint(10, 20)
print("爬去: " + url + " 需要时间 : " + str(k))
sites_index += 1
# print(url)
time.sleep(k)
print('退出 ', url)
for i in range(100):
threading.Thread(target=test).start()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有