python threading.Barrier栅栏对象

当栅栏中所有线程都已经调用了这个函数,它们将同时被释放。如果提供了 timeout 参数,这里的 timeout 参数优先于创建栅栏对象时提供的 timeout 参数。

即当2个线程同时到达 wait 方式继续向下执行。

import threading
import time

b = threading.Barrier(2, timeout=5)


def task1():
    time.sleep(3)
    print('start task1')
    b.wait()
    print('end task1')


def task2():
    time.sleep(1)
    print('start task2')
    b.wait()
    print('end task2')


threading.Thread(target=task1).start()
threading.Thread(target=task2).start()
print('over')
start task2
start task1
end task1
end task2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值