zoukankan      html  css  js  c++  java
  • Python学习之路:信号量

     1 import threading,time
     2 
     3 def run(n):
     4     semaphore.acquire()
     5     time.sleep(1)
     6     print("run the thread: %s
    " %n)
     7     semaphore.release()
     8 
     9 if __name__ == '__main__':
    10 
    11     num= 0
    12     semaphore  = threading.BoundedSemaphore(5) #最多允许5个线程同时运行
    13     for i in range(22):
    14         t = threading.Thread(target=run,args=(i,))
    15         t.start()
    16 
    17 while threading.active_count() != 1:
    18     pass #print threading.active_count()
    19 else:
    20     print('----all threads done---')
    21     print(num)
    信号量
  • 相关阅读:
    JUC并发工具包之Semaphore
    Linux命令
    uWSGI
    数据库 MySQL 练习
    c++
    c++ 初阶
    Git
    MySQl 和 Redis
    MySQL 查询
    MySQL 命令
  • 原文地址:https://www.cnblogs.com/xiaobai005/p/8676605.html
Copyright © 2011-2022 走看看