首页 > 其他 > 详细

event事件

时间:2018-06-19 00:40:49      阅读:322      评论:0      收藏:0      [点我收藏+]

#!/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()

event事件

原文:https://www.cnblogs.com/fmgao-technology/p/9196768.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!