新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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()
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()
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()