风-fmgao

导航

event事件

#!/usr/bin/env python
# encoding: utf-8  
# Date: 2018/6/18


# from threading import Thread,Event
# import time
# event = Event()
# # event.wait()
# # event.set()
#
# def student(name):
#     print('学生%s 正在听课' % name)
#     event.wait(3)  # 设置时间之后不必等待7秒
#     print('学生%s 课间活动' % name)
#
#
# def teacher(name):
#     print('老师%s 正在授课' % name)
#     time.sleep(7)
#     event.set()
#
#
# if __name__ == '__main__':
#     stu1 = Thread(target=student, args=('alex', ))
#     stu2 = Thread(target=student, args=('wxx', ))
#     stu3 = Thread(target=student, args=('yxx', ))
#     t1 = Thread(target=teacher, args=('egon', ))
#
#     stu1.start()
#     stu2.start()
#     stu3.start()
#     t1.start()

from threading import Thread,Event,currentThread
import time

event = Event()


def conn():
    n = 0
    while not event.isSet():
        if n == 3:  # 设置最多链接3次
            print('%s try too many times' % currentThread().getName())
            return
        print('%s try %s' % (currentThread().getName(), n))
        event.wait(0.5)
        n += 1
    print('%s is connected' % currentThread().getName())


def check():
    print('%s is checking' % currentThread().getName())
    time.sleep(5)
    event.set()


if __name__ == '__main__':
    for i in range(3):
        t = Thread(target=conn)
        t.start()
    t = Thread(target=check)
    t.start()

posted on 2018-06-18 23:39  风-fmgao  阅读(97)  评论(0编辑  收藏  举报