[轉載] 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.pswp.cn/news/540629.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540629.shtml
英文地址,請注明出處:http://en.pswp.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;鍵盤、鼠標和游…