Python3.X thread exemplary method using semaphore

Semaphore semaphore is a variable that controls access to a public resource or critical section. Semaphore maintains a counter, specify the number of threads that can simultaneously access a resource or enter the critical region. The following article is mainly to introduce the use of thread on Python3.X semaphore, a friend in need can refer to learn, let's look at it.
Foreword

Python recent study, found that basic knowledge about the thread semaphore, in-depth understanding of python thread will be of great help. Therefore, this article will introduce you Python3.X thread semaphore use the following word does not say, take a look at the detailed description together:

Method Example

Threads, semaphores are used primarily to maintain a limited resource, so that the thread uses the resource at a certain time only the designated number

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

Can be found in the program, only two crawler is always active Here Insert Picture Description
, our new python learning sites , to see how old the program is to learn! From basic python script, reptiles, django, data mining, programming techniques, work experience, as well as senior careful study of small python partners to combat finishing zero-based information projects! The method every day to explain the timing of Python programmers technology, learn and share some small details need to pay attention to the
summary

That's all for this article, I hope the content of this paper, we study or work can bring some help

Published 51 original articles · won praise 17 · views 30000 +

Guess you like

Origin blog.csdn.net/haoxun02/article/details/104418958