Python 信号量的使用
线程中,信号量主要是用来维持有限的资源,使得在一定时间使用该资源的线程只有指定的数量
# -*- 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()
可以发现该程序中,永远只有2个爬虫是处于活动状态
相关推荐
starinshy 2020-11-10
archimedes 2020-11-05
hackerlpy 2020-09-25
小菜鸟的代码世界 2020-06-17
猛禽的编程艺术 2020-06-08
神龙 2020-06-07
jackalwb 2020-06-05
安得情怀似旧时 2020-04-20
farwang 2020-04-20
paopao00 2020-04-09
isHooky 2020-04-09
reallyr 2020-02-23
RayCongLiang 2019-12-29
pointfish 2020-01-03
wanggongzhen 2020-01-09
wbczyh 2020-01-07
laityc 2019-12-15
hahalale 2019-11-30
wordmhg 2019-11-30