[转载] Python3.X 线程中信号量的使用方法示例

参考链接: 示例说明Python2.x和Python3.x之间的重要区别

信号量semaphore 是一个变量,控制着对公共资源或者临界区的访问。信号量维护着一个计数器,指定可同时访问资源或者进入临界区的线程数。下面这篇文章主要给大家介绍了关于Python3.X 线程中信号量的使用方法,需要的朋友可以参考借鉴,下面来一起看看吧。 前言 

最近在学习python,发现了解线程信号量的基础知识,对深入理解python的线程会大有帮助。所以本文将给大家介绍Python3.X线程中信号量的使用方法,下面话不多说,来一起看看详细的介绍: 

方法示例 

线程中,信号量主要是用来维持有限的资源,使得在一定时间使用该资源的线程只有指定的数量 

# -*- 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个爬虫是处于活动状态 推荐我们的python学习基地,看老程序是如何学习的!从基础的python脚本、爬虫、django、数据挖掘等编程技术,工作经验,还有前辈精心为学习python的小伙伴整理零基础到项目实战的资料,!每天都有程序员定时讲解Python技术,分享一些学习的方法和需要留意的小细节 总结 

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540629.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从流程的自动化中获得最大价值的10种方式

流程自动化很好,如果它可以节省时间并减少错误。但是如果它不能在业务流程中“很好地契合”,那么会难以得到普及。问问有谁没有对语音助手感到伤脑筋。 所幸的是,某些最佳实践让你可以从流程自动化中获得最大价值,以下就是其中的1…

java中null是常量吗_C_NULL Julia中的常量

java中null是常量吗Julia| C_NULL常数 (Julia | C_NULL Constant) C_NULL is a constant of Ptr{Nothing} type in Julia programming language, it represents the null pointer value, which is used for C Null Pointer while calling external code. C_NULL是Julia编程语言…

[转载] Python京东抢购

参考链接: 从Python获取输入 Python京东抢购 分析其中提交信息接口的参数,可以成功抢购商品,并且可以提交订单。。。。2018年7月17日 提交信息的获取 直接提交信息对post提交分析其中的参数。 经过分析参数大多数在:https…

6.04 从字符串中删除不需要的字符

需求:删除所有的0和元音字母。 select ename,replace(replace(replace(replace(replace(ename,A,),E,),I,),O,),U,) as stripped1,sal,replace(sal,0,) stripped2from emp;转载于:https://www.cnblogs.com/liang545621/p/7518766.html

Scala分号

Scala分号 (Scala semicolons) A semicolon or semi-colon (;) is a punctuation mark in programming, it is used to separate multiple lines of code. It is common in major programming languages like C, C, Java, Pascal. In modern programming languages like Python…

[转载] python通过adb获取android手机耗电量

参考链接: 从Python中控制台获取输入 把开发者模式打开,激活 adb 调试,然后可以使用以下python代码获取安卓手机的耗电量 # -*- coding: utf-8 -*- import re import os def getSelectDevice(): pip os.popen(adb devices) result pip.…

ES6之主要知识点(二) 变量的解构赋值。默认值

引自http://es6.ruanyifeng.com/#docs/destructuring 数组解构赋值默认值对象解构赋值用途1.数组的解构赋值 let [a, b, c] [1, 2, 3]; let [foo, [[bar], baz]] [1, [[2], 3]]; foo // 1 bar // 2 baz // 3let [ , , third] ["foo", "bar", "baz&…

python无符号转有符号_Python | 散布符号

python无符号转有符号There are multiple types of Scatter Symbols available in the matplotlib package and can be accessed through the command marker. In this article, we will show some examples of different marker types and also present a list containing all…

[转载] 基于LSTM的股票预测模型_python实现_超详细

参考链接: 从Python获取输入 文章目录 一、背景二、主要技术介绍1、RNN模型2、LSTM模型3、控制门工作原理四、代码实现五、案例分析六、参数设置七、结论完整程序下载 一、背景 近年来,股票预测还处于一个很热门的阶段,因为股票市场的波动…

shell -eom_EOM的完整形式是什么?

shell -eomEOM:消息结尾 (EOM: End Of Message) EOM is an abbreviation of "End Of Message". EOM是“消息结尾”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is also written as Eom or eom. It is written at …

在eclipse中启动Tomcat访问localhost:8080失败项目添加进Tomcat在webapp中找不到

软件环境:Eclipse oxygen, Tomcat8.5 #在eclipse中启动Tomcat访问localhost:8080失败 在eclipse中配置tomcat后,打开tomcat后访问localhost:8080后无法出现登陆成功的界面,即无法出现下面的界面 在eclipse中的servers状态栏中双击tomcat&…

[转载] 【基础教程】Python input()函数:获取用户输入的字符串

参考链接: 从Python中控制台获取输入 input() 是 Python 的内置函数,用于从控制台读取用户输入的内容。input() 函数总是以字符串的形式来处理用户输入的内容,所以用户输入的内容可以包含任何字符。 input() 函数的用法为: str…

程序员简历工作模式_简历的完整形式是什么?

程序员简历工作模式简历:简历 (CV: Curriculum Vitae) The CV is an abbreviation of Curriculum Vitae. It is a written outline summary of a persons educational training and qualifications and his other experiences. It is an absolute profile of a cand…

[转载] Python新手写出漂亮的爬虫代码1——从html获取信息

参考链接: Python中从用户获取多个输入 Python新手写出漂亮的爬虫代码1 初到大数据学习圈子的同学可能对爬虫都有所耳闻,会觉得是一个高大上的东西,仿佛九阳神功和乾坤大挪移一样,和别人说“老子会爬虫”,就感觉特别…

在Scala中设置&()方法

Scala中的Set&()方法 (The Set &() method in Scala) The &() method in the Set is used to create a new set in Scala. This new set created contains all elements from the other two sets that are common for both of the given sets i.e. new set …

[转载] python与c/c++相比有哪些优势

参考链接: Python输入和C, Java速度对比 理论上,python的确比C/C慢(我对Java的开发没有经验,无法评论)。这一点不用质疑。 C/C是编绎语言,直接使用的是机器指令,而python总是跑在的虚拟机上&am…

清空日志的三种方法

方法一:echo "" >test.log方法二:> test.log方法三:cat /dev/null >test.log转载于:https://www.cnblogs.com/liang545621/p/7528509.html

splat net_Ruby中的Splat参数

splat netRuby Splat参数 (Ruby Splat Arguments) We have learnt how to work with methods in Ruby? We are very well aware of the fact that methods may or may not consume any arguments. Let us discuss the methods which consume argument or have a predefined ar…

ajax的访问 WebService 的方法

转自原文 ajax的访问 WebService 的方法 如果想用ajax进行访问 首先在web.config里进行设置 添加在 <webServices> <protocols> <add name "HttpPost" /> <add name "HttpGet" /> </protocols> </webServices> <s…

[转载] 使用DirectInput进行交互

参考链接&#xff1a; input()函数中的漏洞– Python2.x 使用DirectInput进行交互&#xff08;1&#xff09; DirectX 2008-08-10 15:11:34 阅读169 评论0 字号&#xff1a;大 中 小 订阅 输入设备简介 计算机通常使用三种输入设备&#xff1a;键盘、鼠标和游…