python thread semaphore semaphore (33)

   

    Through the front of the  thread mutex Lock  /   thread events for Event  /  for condition Condition thread condition variable  /  thread timer timer  to explain, I believe you already have some knowledge of threading threading module, execute multiple threads can indeed improve the efficiency of the program , but not the number of threads the better, probably for the computer, you just run 20 to 30 threads may not have much impact, at the same time if you run thousands or even tens of it? I am sure you will have a direct computer paralysis ......

 

ear

 

A semaphore principle .semaphore

    Multithreaded run, can improve the efficiency of the program, but not better threads, semaphores and semaphore can be controlled while the number of running threads, the thread start (consumed amount signal) internal counter is automatically decremented by a built-in counter, thread end (release semaphore) plus a built-in counter is automatically; built-in counter is zero, start thread is blocked until the end of this or any other thread until the end of the thread;

 

Oh yo

 

Two correlation function describes the semaphore .semaphore

    acquire () - the amount of consumption signal, a built-in counter is decremented;

    release () - releases the semaphore, a built-in counter is incremented;

    Semaphore in the semaphore has a built-in counter, the number of threads of control, Acquire () consumes an amount of the signal, the counter is automatically decremented; Release () releases the semaphore, the counter is incremented by one; when the counter is zero, Acquire ( ) call is blocked until the release () until the semaphore is released.

learning python

 

Signal amount three .semaphore

    Create multiple threads, the same time limit the run up to five threads, sample code as follows:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com
 
@File:python_semaphore.py
@Time:2019/10/23 21:25
 
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
 
# 导入线程模块
import threading
# 导入时间模块
import time
 
# 添加一个计数器,最大并发线程数量5(最多同时运行5个线程)
semaphore = threading.Semaphore(5)
 
def foo():
    semaphore.acquire()    #计数器获得锁
    time.sleep(2)   #程序休眠2秒
    print("当前时间:",time.ctime()) # 打印当前系统时间
    semaphore.release()    #计数器释放锁
 
 
if __name__ == "__main__":
 
    thread_list= list()
    for i in range(20):
        t=threading.Thread(target=foo,args=()) #创建线程
        thread_list.append(t)
        t.start()  #启动线程
 
    for t in thread_list:
        t.join()
 
    print("程序结束!")

 

    输出结果:

当前时间: Wed Oct 23 22:21:59 2019
当前时间: Wed Oct 23 22:21:59 2019
当前时间: Wed Oct 23 22:21:59 2019
当前时间: Wed Oct 23 22:21:59 2019
当前时间: Wed Oct 23 22:21:59 2019
当前时间: Wed Oct 23 22:22:01 2019
当前时间: Wed Oct 23 22:22:01 2019
当前时间: Wed Oct 23 22:22:01 2019
当前时间: Wed Oct 23 22:22:01 2019
当前时间: Wed Oct 23 22:22:01 2019
当前时间: Wed Oct 23 22:22:03 2019
当前时间: Wed Oct 23 22:22:03 2019
当前时间: Wed Oct 23 22:22:03 2019
当前时间: Wed Oct 23 22:22:03 2019
当前时间: Wed Oct 23 22:22:03 2019
Current Time: Wed Oct 23 22:22:05 2019 
Current Time: Wed Oct 23 22:22:05 2019 
Current Time: Wed Oct 23 22:22:05 2019 
Current Time: Wed Oct 23 22:22:05 2019 
Current time : Wed Oct 23 22:22:05 2019 
program ends!

 

    It can be seen according to the log printing, at the same time only five threads running, after an interval of two seconds starts five threads again until the end of all 20 threads run; if no semaphore Semapaore, create a thread directly start (), output of all time is the same, the problem is relatively simple, you can go to test their own look!

 

learning python

 

you may also like:

    1.python thread creation and parameter passing

    2.python dictionary derivations

    3.python list comprehensions

    4.python return logical operators

    5.python variable length parameters * argc, ** kargcs

 

    Reproduced please specify: ape say Python  »  Python thread semaphore semaphore

 

Technical exchanges, business cooperation please contact bloggers
Scan code or search: ape say python
No public python tutorial
Ape say python
No. sweep the micro-channel public concern

Guess you like

Origin www.cnblogs.com/shuopython/p/11961542.html