ThreadingBarrier代码笔记-创新互联
Barrier简单演示
import threading
import logging
FORMAT = '%(threadName)s %(thread)d %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
def worker(barrier:threading.Barrier):
logging.info("waiting for {} threading. ".format(barrier.n_waiting))
try:
barrier_id = barrier.wait()
logging.info('after barrier {}'.format(barrier_id)) # 显示等待的barrier实例数量
except threading.BrokenBarrierError:
logging.info('Broken Barrier')
barrier = threading.Barrier(3)
for x in range(5):
threading.Event().wait(2)
threading.Thread(target=worker, name='worker-{}'.format(x), args=(barrier, )).start()
Barrier之 broken的代码演示
import threading
import logging
FORMAT = '%(threadName)s %(thread)d %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
def worker(barrier:threading.Barrier):
logging.info('waitting for {} threads'.format(barrier.n_waiting))
try:
barrier_id = barrier.wait()
logging.info('after barrier {}'.format(barrier_id))
except threading.BrokenBarrierError:
logging.info('broken barrier')
barrier = threading.Barrier(3)
for i in range(0, 9):
if i == 2:
barrier.abort()
elif i == 6:
barrier.reset()
threading.Event().wait(2)
threading.Thread(target=worker, args=(barrier, )).start()
Barrier之 wait方法超时演示
import threading
import logging
FORMAT = '%(threadName)s %(thread)d %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
def worker(barrier:threading.Barrier, i:int):
logging.info('waitting for {} threads'.format(barrier.n_waiting))
try:
if i < 3:
barrier_id = barrier.wait(1) # 设定超时时间
else:
if i == 6:
barrier.reset() # 恢复屏障
barrier_id = barrier.wait()
logging.info('after barrier {}'.format(barrier_id))
except threading.BrokenBarrierError:
logging.info('broken barrier')
barrier = threading.Barrier(3)
for i in range(0, 9):
threading.Event().wait(2)
threading.Thread(target=worker, args=(barrier, i)).start()
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
10年积累的成都做网站、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站策划后付款的网站建设流程,更有托克逊免费网站建设让你可以放心的选择与我们合作。文章名称:ThreadingBarrier代码笔记-创新互联
标题网址:http://ybzwz.com/article/dgopds.html