Home  >  Article  >  Backend Development  >  Detailed explanation on the use of semaphores in Python3.X threads

Detailed explanation on the use of semaphores in Python3.X threads

黄舟
黄舟Original
2017-07-24 15:20:211756browse

Semaphore is a variable that controls access to public resources or critical sections. The semaphore maintains a counter that specifies the number of threads that can simultaneously access the resource or enter the critical section. The following article mainly introduces you to the use of semaphores in Python3.X threads. Friends in need can refer to it. Let’s take a look together.

Preface

I have been learning python recently and found that understanding the basic knowledge of thread semaphores will be of great help in understanding python threads in depth. Therefore, this article will introduce to you how to use semaphores in Python3.

#In threads, semaphores are mainly used to maintain limited resources, so that only a specified number of threads can use the resource at a certain time

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

It can be found that in this program, only 2 crawlers are always active


##Summary

The above is the detailed content of Detailed explanation on the use of semaphores in Python3.X threads. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn