This is the seventh day of my participation in the First Challenge 2022. For details: First Challenge 2022.

Official Python column # 55, stop! Don’t miss this zero-based article!

The Condition class was briefly introduced in the previous section. This class has several special functions.

After one referee acquires the lock and notify_all is executed, the other player threads begin to prepare.

This is what the previous article implemented.

However, the referee thread entered the waiting state, the following 3/2/1 did not have a chance to call out, the other player threads also had to wait.

Timed wait: wait(one time)

We know that after the Condition object calls wait, the thread enters the wait.

While both the athlete and the referee are waiting, there is no thread outside to tell the referee to start Shouting

#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: @xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: thread_condition.py # @Project: hello import threading import time condition = threading.Condition() read_condition = threading.Condition() counter = {"value": 0} def wait_and_run(): condition.acquire() print("%s wait for signal " % threading.current_thread().name) counter['value'] += 1 Condition. Wait () print("%s run" % threading.current_thread().name) time.sleep(0.5) print("%s completed" % threading.current_thread().name) condition.release() threads = [] for i in range(1, 11): Thread.append (t) def fire(): threading.thread.append (t) def fire(): threading.thread.append (t) def fire(): Condition. Acquire () print(" prepare ") condition. Notify_all () condition. ) condition.wait(1) print("2..." ) condition.wait(1) print("1..." Print ("fire") condition. Notify_all () condition. Wait (5) threading. Start () print() for t in threads: t.start() for t in threads: t.start() print() for t in threads: t.start() print() for T in threads: t.start() print() for T in threads: t.start() print() for T in threads: t.start() print()Copy the code

Let’s take a look at the results:

In the first stage, the referee begins to yell 3/2/1 after the players have finished waiting. Because the wait method waits for the time to arrive.

Then in the second phase, after the referee thread calls notify_all, the runner starts running.

The runners finished one by one and the race was over.

Code parsing

This code is different from the previous article except that some print output, the important change is the wait function pass time.

By this design, we do not need a third thread to tell the referee to continue.

This is similar to the real world scenario: on the field, the referee observes that all the players are ready and then fires the gun (similar to 3/2/1).

conclusion

I wonder if any of you who are reading this have thought of this implementation?

There are a number of tools that can be used to implement an equivalent scenario for a track and field scenario. This article will show you an example of how to use Condition by studying this code.

For those who like Python, please check out the Python Basics section or the Python Getting Started to Master Section

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!