美文网首页
python 多线程信号量semaphore

python 多线程信号量semaphore

作者: lxc198 | 来源:发表于2018-12-13 16:20 被阅读0次
import threading
import logging
import time


def worker(s):
  logging.debug('starting')
  with s:
      name = threading.current_thread().name
      print(name)
      time.sleep(2)


logging.basicConfig(
  level=logging.DEBUG,
  format='%(asctime)s (%(threadName)-2s) %(message)s',
)

s = threading.Semaphore(5)

for i in range(100):
  t = threading.Thread(
      target=worker,
      name=str(i),
      args=(s,),
  )
  t.start()

相关文章

网友评论

      本文标题:python 多线程信号量semaphore

      本文链接:https://www.haomeiwen.com/subject/sadbhqtx.html