Python多線程-Barrier(障礙對象)

Barrier(parties, action=None, timeout=None)

每一個線程經過調用wait()嘗試經過障礙,並阻塞,直到阻塞的數量達到parties時,阻塞的線程被同時所有釋放。
action是一個可調用對象,當線程被釋放時,其中一個線程會首先調用action,以後再跑本身的代碼。
timeout時默認的超時時間。python

方法:
wait(timeout=None)
嘗試經過障礙並阻塞。
返回值是一個在0parties-1範圍內的整數,每一個線程都不一樣。
其中一個線程在釋放以前將調用action。若是此調用引起錯誤,則障礙將進入斷開狀態。
若是等待超時,障礙也將進入斷開狀態。
若是在線程等待期間障礙斷開重置,此方法可能會引起BrokenBarrierError錯誤。線程

reset()
重置障礙,返回默認的空狀態,即當前阻塞的線程從新來過。見例二code

abort()
將障礙置爲斷開狀態,這將致使已調用wait()或以後調用wait()引起BrokenBarrierError。見例三orm

屬性:
partier
經過障礙所需的線程數。對象

n_waiting
當前在屏障中等待的線程數utf-8

broken
若是屏障處於斷開狀態,則返回Trueit

實例

例一:io

# -*- coding:utf-8 -*-
import threading
import time


def open():
    print('人數夠了, 開門!')


barrier = threading.Barrier(3, open)


class Customer(threading.Thread):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.n = 3

    def run(self):
        while self.n > 0:
            self.n -= 1
            print('{0}在等着開門.'.format(self.name))
            try:
                barrier.wait(2)
            except threading.BrokenBarrierError:
                pass
            print('開門了, go go go')


if __name__ == '__main__':
    t1 = Customer(name='A')
    t2 = Customer(name='B')
    t3 = Customer(name='C')
    t1.start()
    t2.start()
    t3.start()

運行結果:form

A在等着開門.
B在等着開門.
C在等着開門.
人數夠了, 開門!
開門了, go go go
開門了, go go go
開門了, go go go
C在等着開門.
A在等着開門.
B在等着開門.
人數夠了, 開門!
開門了, go go go
開門了, go go go
開門了, go go go
...

例二:class

# -*- coding:utf-8 -*-
import threading
import time


def open():
    print('人數夠了, 開門!')


barrier = threading.Barrier(3, open)


class Customer(threading.Thread):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.n = 3

    def run(self):
        while self.n > 0:
            self.n -= 1
            print('{0}在等着開門.'.format(self.name))
            try:
                barrier.wait(2)
            except threading.BrokenBarrierError:
                continue
            print('開門了, go go go')


class Manager(threading.Thread):
    def run(self):
        print('前面幾個排隊的不算,從新來')
        barrier.reset()


if __name__ == '__main__':
    t1 = Customer(name='A')
    t2 = Customer(name='B')
    t3 = Customer(name='C')
    tm = Manager()
    t1.start()
    t2.start()
    tm.start()
    t3.start()

運行結果:

A在等着開門.
B在等着開門.
前面幾個排隊的不算,從新來
A在等着開門.
B在等着開門.
C在等着開門.
人數夠了, 開門!
開門了, go go go
開門了, go go go
開門了, go go go
A在等着開門.
C在等着開門.
B在等着開門.
人數夠了, 開門!
開門了, go go go
開門了, go go go
開門了, go go go
C在等着開門.

例三:

# -*- coding:utf-8 -*-
import threading


def open():
    print('人數夠了, 開門!')


barrier = threading.Barrier(3, open)


class Customer(threading.Thread):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.n = 3

    def run(self):
        while self.n > 0:
            self.n -= 1
            print('{0}在等着開門.'.format(self.name))
            try:
                barrier.wait(2)
            except threading.BrokenBarrierError:
                print('今天好像不開門了,回家.')
                break
            print('開門了, go go go')


class Manager(threading.Thread):
    def run(self):
        print('老闆跟小姨子跑了,不開門了!')
        barrier.reset()


if __name__ == '__main__':
    t1 = Customer(name='A')
    t2 = Customer(name='B')
    t3 = Customer(name='C')
    tm = Manager()
    t1.start()
    t2.start()
    tm.start()
    t3.start()

運行結果:

A在等着開門.
B在等着開門.
老闆跟小姨子跑了,不開門了!
今天好像不開門了,回家.
今天好像不開門了,回家.
C在等着開門.
今天好像不開門了,回家.
相關文章
相關標籤/搜索