zoukankan      html  css  js  c++  java
  • python用event实现门禁卡功能

    有一个event使用的例子,员工进公司门要刷卡, 我们这里设置一个线程是“门”, 再设置几个线程为“员工”,员工看到门没打开,就刷卡,刷完卡,门开了,员工就可以通过。

     1 #_*_coding:utf-8_*_
     2 __author__ = 'Alex Li'
     3 import threading
     4 import time
     5 import random
     6 
     7 def door():
     8     door_open_time_counter = 0
     9     while True:
    10         if door_swiping_event.is_set():
    11             print("33[32;1mdoor opening....33[0m")
    12             door_open_time_counter +=1
    13 
    14         else:
    15             print("33[31;1mdoor closed...., swipe to open.33[0m")
    16             door_open_time_counter = 0 #清空计时器
    17             door_swiping_event.wait()
    18 
    19 
    20         if door_open_time_counter > 3:#门开了已经3s了,该关了
    21             door_swiping_event.clear()
    22 
    23         time.sleep(0.5)
    24 
    25 
    26 def staff(n):
    27 
    28     print("staff [%s] is comming..." % n )
    29     while True:
    30         if door_swiping_event.is_set():
    31             print("33[34;1mdoor is opened, passing.....33[0m")
    32             break
    33         else:
    34             print("staff [%s] sees door got closed, swipping the card....." % n)
    35             print(door_swiping_event.set())
    36             door_swiping_event.set()
    37             print("after set ",door_swiping_event.set())
    38         time.sleep(0.5)
    39 door_swiping_event  = threading.Event() #设置事件
    40 
    41 
    42 door_thread = threading.Thread(target=door)
    43 door_thread.start()
    44 
    45 
    46 
    47 for i in range(5):
    48     p = threading.Thread(target=staff,args=(i,))
    49     time.sleep(random.randrange(3))
    50     p.start()
    View Code
  • 相关阅读:
    POJ3094 UVALive3594 HDU2734 ZOJ2812 Quicksum【进制】
    UVALive5583 UVA562 Dividing coins
    POJ1979 HDU1312 Red and Black【DFS】
    POJ1979 HDU1312 Red and Black【DFS】
    POJ2386 Lake Counting【DFS】
    POJ2386 Lake Counting【DFS】
    HDU4394 Digital Square
    HDU4394 Digital Square
    UVA213 UVALive5152 Message Decoding
    UVA213 UVALive5152 Message Decoding
  • 原文地址:https://www.cnblogs.com/zijue/p/9810978.html
Copyright © 2011-2022 走看看