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()

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

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

相关文章

Codeforces Round #343 (Div. 2) A. Far Relative’s Birthday Cake 水题

A. Far Relative’s Birthday Cake题目连接: http://www.codeforces.com/contest/629/problem/A Description Doors family is going celebrate Famil Doorss birthday party. They love Famil Door so they are planning to make his birthday cake weird! The cak…

fedora操作系统优缺点_不同类型的操作系统的优缺点

fedora操作系统优缺点There are following types of Operating systems: 有以下几种操作系统: Batch Operating System 批处理操作系统 Time-Sharing Operating System 分时操作系统 Distributed Operating System 分布式操作系统 Network Operating System 网络操…

python radians函数_Python3 radians() 函数

Python3 radians() 函数描述radians() 方法将角度转换为弧度。语法以下是 radians() 方法的语法:import mathmath.radians(x)注意:radians()是不能直接访问的,需要导入 math 模块,然后通过 math 静态对象调用该方法。参数x -- 一个数值。返…

android decorView详解

摘要 一、DecorView为整个Window界面的最顶层View。 二、DecorView只有一个子元素为LinearLayout。代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域。 三、LinearLayout里有两个FrameLayout子元素。 (20)为标题栏显示界面。只有一个…

c语言1+2+3+4+5_C程序来计算系列1+(1 + 2)+(1 + 2 + 3)+(1 + 2 + 3 + 4)+ ... +(1 + 2 + 3 + ... + n)...

c语言12345The series is: 1(12) (123) (1234) ... (123...n), we have to find out the sum up to N terms. 该序列是: 1(1 2)(1 2 3)(1 2 3 4) ... (1 2 3 ... n) ,我们有找出N个项之和。 Solution: 解: We know the sum of n…

3dmax批量导入obj_ArcGIS 与 3DMax 结合建模

整体技术思路是将项目区二维的CAD测绘底图,通过整理导入到3DMax中,根据CAD底图为基础,绘制三维数字模型。利用Photoshop 平面图像处理软件,对现场采集的照片进行修整,为三维模型制作表面贴图。最终把贴好材质的三维楼体…

iOS对象属性详解

oc对象的一些属性: retain,strong, copy,weak,assign,readonly, readwrite, unsafe_unretained下面来分别讲讲各自的作用和区别:retain,计数器加1, (增加一个指向内存的指针) 对应release(计数器&#xff0…

给定一个由n个数字组成的数组,请检查是否存在重复项

This is a searching problem which can be solved using brute force approach. But here we are going to see use of hash table to solve such searching problems at lower time complexity. 这是一个搜索问题,可以使用蛮力方法解决。 但是在这里,我…

字符串乘一个数_【思维拓展】三位数乘两位数,构造最大积和最小积

前面袁老师给大家讲了一个重要结论,并运用这个结论来解决问题,构造两位数乘两位数最大积和最小积的问题。今天,更进一步,三位数乘两位数中,如何构造最大积和最小的积?【问题引入】用9、8、6、5、4这五个数字…

ios 微信支付

服务器签名版本 官方已经是建议使用服务器签名来接入微信支付,实际上从安全上考虑,确实是每个客户端不应该知道RAS密钥,也不需要每个客户端都写一遍签名的算法。 服务端接入流程文档:https://pay.weixin.qq.com/wiki/doc/api/app.…

Python | 基于参数和返回值的功能分类

There are following types of the functions based on their parameters and return values: 根据函数的参数和返回值,有以下几种类型的函数: Function with no argument and no return value 没有参数也没有返回值的函数 Function with no argument …

macos可以升级到指定版本吗_承装承修承试可以跨级升级吗?

在建筑行业,通常我们所说的承装承修承试,也就是指承装(修、试)电力设施许可证。承装(修、试)电力设施许可证的功能作用相当于建筑资质,企业需要办理许可证后才能承接电力设施的安装、维护、调试等工程项目。承装(修、试)电力设施许可证可以办…

C#对config配置文件的管理

应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是App.Config(ExeName.exe.config)。 配置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.NET…

16位的数字高字节和低字节_显示掩盖较低和较高的16位数字半字节| 8086微处理器...

16位的数字高字节和低字节Problem: To show masking of lower and higher nibbles of 16bit number using 8086 Microprocessor. 问题:使用8086微处理器显示对16位数字的较高半字节和较低半字节的屏蔽。 Assumption: 假设: Number is stored at memory…

一个事物两个方面的对比举例_顶管施工也有讲究,两个方面一个个来

顶管施工其实就是我们平时说的不开挖或者非开挖施工啦,其原理是借助于主顶油缸及管道间、中继间等推力,把工具管或掘进机从工作坑内穿过土层一直推进到接收坑内吊起。管道紧随工具管或掘进机后,埋设在两坑之间。为了响应中央的号召&#xff1…

SQLServer中的死锁的介绍

简介 什么是死锁? 我认为,死锁是由于两个对象在拥有一份资源的情况下申请另一份资源,而另一份资源恰好又是这两对象正持有的,导致两对象无法完成操作,且所持资源无法释放。 什么又是阻塞? 阻塞是由于资源不…

c语言位运算符的使用_C语言程序使用位运算符检查给定数字是否为回文

c语言位运算符的使用Problem statement: Write a C program to check whether a number (binary representation) is palindrome or not using bitwise operators. Maximum input is 255.. 问题陈述:编写一个C程序以使用按位运算符检查数字(二进制​​表示形式)是否…

解析取值_圆锥曲线——高中解析几何全归纳

这是一系列文章,我将在接下来了80多天,尽力把理科比较难的大题题型全部归纳一下然后在最后我会告诉做解析几何的窍门,让你的解析几何不再没有头绪,拿到既可做全文干货,不掺水,可以说总结了解析几何中你能遇…

Tomcat版本与Servlet、JSP等版本的支持关系

2019独角兽企业重金招聘Python工程师标准>>> 转载于:https://my.oschina.net/garyxiong/blog/624619

php 随机两位小数数_使用8086微处理器找出两个8位N数数组

php 随机两位小数数Problem statement: 问题陈述: Write a program in 8086 microprocessor to find out the sum of two arrays of 8-bit n numbers, where size “n” is stored at offset 500 and the numbers of first array are stored from offset 501 and t…