在一个线程运行时阻塞其他线程

2024-05-19 19:48:38 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有两种类型的线程

  1. 每x分钟运行一次的单线程。我们称之为线程

  2. 多线程一直在运行。B线程当一个线程执行某个操作()时,我希望所有的B线程等待完成,然后恢复它们。我不知道该用什么。

我试着使用threading.Conditionwait()/notifyAll(),但它没有按我想要的那样工作。一旦我把条件放进去,它就会像synco线程一样一个接一个地处理。我要他们自由奔跑。你知道吗

这是一个示例代码,我试着把它们wait(),然后通知它们,但它会像join()那样逐个执行。不知道我们怎么了。你知道吗

class ...
check = True
def xxx(self,g,con):
  for i in range(3):
    with con:
      if self.check:
        con.wait()
      self.check = False
      time.sleep(3)
      print(g)

con = threading.Condition()
threading.Thread(target=xxx,args=('a',con,)).start()
threading.Thread(target=xxx,args=('b',con,)).start()
threading.Thread(target=xxx,args=('c',con,)).start()
time.sleep(2)
con.notifyAll()

Tags: selftargettimecheckargssleepcondition线程
1条回答
网友
1楼 · 发布于 2024-05-19 19:48:38

Question: Blocking other Threads while one Thread is running

本例不使用threading.Condition(),而是使用threading.Barrier(...)。你知道吗


docs.python.org使用的模块:


import time, threading
from threading import BrokenBarrierError

def worker_A(g, terminate, barrier):
    # Counter to simulate conditional workload
    do_something = 3

    while not terminate.is_set():
        if do_something == 0:
            # Reset the barrier and wait until n_waiting == 2
            barrier.reset()
            while not terminate.is_set() and barrier.n_waiting < 2:
                time.sleep(0.5)

            # Now the other Threads waiting at the barrier
            # Simulate worklaod ...
            print('worker_A barrier.broken={} n_waiting={}'
                .format(barrier.broken, barrier.n_waiting))
            time.sleep(3)

            # Call the third barrier.wait to release the barrier
            try:
                barrier.wait()
            except BrokenBarrierError:
                pass

            # Reset counter to restart simulate conditional workload
            do_something = 3
        else:
            # Count down and give the other threads a timeslice
            do_something -= 1
            time.sleep(0.5)

def worker_B(g, terminate, barrier):
    while not terminate.is_set():
        # Simulate workload ...
        print('worker_B({})'.format(g))
        time.sleep(1)

        # Block at barrier.wait() if the barrier is NOT in the broken state
        try:
            barrier.wait()
        except BrokenBarrierError:
            pass

if __name__ == "__main__":
    # Event to terminate all Threads save
    terminate = threading.Event()

    # Barrier to block worker_B Threads
    # We use 3 Threads, therefore init with parties=3
    barrier = threading.Barrier(3)
    barrier.abort()

    # Create and start the Threads
    threads = []
    for t in [(worker_A, 'a'), (worker_B, 'b'), (worker_B, 'c'), ]:
        threads.append(threading.Thread(target=t[0], args=(t[1], terminate, barrier,)))
        threads[-1].start()
        time.sleep(0.2)

    # Simulating MAIN Thread
    time.sleep(20)

    # Set the `terminate` Event to True, 
    # and abort the barrier to force all Threads to terminate
    print('Terminate...')
    terminate.set()
    barrier.abort()

    # Wait until all Threads terminated
    for t in threads:
        t.join()

    print('EXIT MAIN')

用Python:3.5测试

相关问题 更多 >