Python notes 12-python Multi-thread events (event)

Source: Internet
Author: User

Objective

Small partners a,b,c around to eat hot pot, when the food is QI, the host said: Open eat! , so the small partners to move chopsticks together, this scene how to achieve

Event (events)

Event: A mechanism for handling events: A built-in flag flag is defined globally, and if the flag value is False, it blocks when the program executes the Event.wait method, and if the flag value is true, then the Event.wait method is no longer blocked.

Event is actually a simplified version of the Condition. The event has no lock and cannot cause the thread to enter a synchronous blocking state.

Event ()

    • Set (): Sets the flag to true and notifies all threads waiting to be blocked to resume running state.

    • Clear (): Sets the flag to false.

    • Wait (timeout): If the flag is true will return immediately, otherwise block the thread to wait for the blocking state, waiting for other threads to call set ().

    • IsSet (): Gets the built-in flag State, which returns TRUE or false.

Event Case 1

Scenario: Small Partners A and B are ready to execute a and B threads when they receive notification event.set ()

# coding:utf-8import threadingimport timeevent = threading.Event()def chihuoguo(name):    # 等待事件,进入等待阻塞状态    print '%s 已经启动' % threading.currentThread().getName()    print '小伙伴 %s 已经进入就餐状态!'%name    time.sleep(1)    event.wait()    # 收到事件后进入运行状态    print '%s 收到通知了.' % threading.currentThread().getName()    print '小伙伴 %s 开始吃咯!'%name# 设置线程组threads = []# 创建新线程thread1 = threading.Thread(target=chihuoguo, args=("a", ))thread2 = threading.Thread(target=chihuoguo, args=("b", ))# 添加到线程组threads.append(thread1)threads.append(thread2)# 开启线程for thread in threads:    thread.start()time.sleep(0.1)# 发送事件通知print '主线程通知小伙伴开吃咯!'event.set()

Operation Result:

Thread-1 已经启动小伙伴 a 已经进入就餐状态!Thread-2 已经启动小伙伴 b 已经进入就餐状态!主线程通知小伙伴开吃咯!Thread-1 收到通知了.小伙伴 a 开始吃咯!Thread-2 收到通知了.小伙伴 b 开始吃咯!
Event Case 2

Scene: When the small partners a,b,c assembled, the guests said: Open eat!

# coding:utf-8import Threadingimport timeevent = Threading. Event () def Chihuoguo (name): # waits for event, enters wait blocking state print '%s has started '% threading.currentthread (). GetName () print ' small partner%s ' After entering the dining state! '%name time.sleep (1) event.wait () # received an event and went into run status print '%s was notified. '% Threading.currentthread (). GetName () PRI NT '%s Small partner%s start eating! '% (Time.time (), Name) class MyThread (threading. Thread): # inherits the parent class threading. Thread def __init__ (self, Name): "' rewrite threading. Thread initializes the content "' Threading. Thread.__init__ (self) self.people = name def run (self): # Write the code you want to execute into the run function, and the thread will run directly after it has been created. "" Override the Run method ' Chihuoguo (self.people) # Execute task print ("QQ Exchange Group: 226296743") print ("End thread:%s"% Threading.currentthr EAD (). GetName ()) # set Thread Group threads = []# Create new Thread thread1 = MyThread ("a") Thread2 = MyThread ("b") Thread3 = MyThread ("C") # Add to thread group th Reads.append (Thread1) threads.append (thread2) Threads.append (thread3) # Open thread for thread in Threads:thread.start () Time.sleep (0.1) # Send event notification print ' The collection is complete, personnel to the QI, open eat! ' Event.set ()

Operation Result:

Thread-1 已经启动小伙伴 a 已经进入就餐状态!Thread-2 已经启动小伙伴 b 已经进入就餐状态!Thread-3 已经启动小伙伴 c 已经进入就餐状态!集合完毕,人员到齐了,开吃咯!Thread-1 收到通知了.1516780957.47 小伙伴 a 开始吃咯!qq交流群:226296743结束线程: Thread-1Thread-3 收到通知了.1516780957.47 小伙伴 c 开始吃咯!Thread-2 收到通知了.qq交流群:2262967431516780957.47 小伙伴 b 开始吃咯!结束线程: Thread-3qq交流群:226296743结束线程: Thread-2

Python notes 12-python Multi-thread events (event)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.