码迷,mamicode.com
首页 > 编程语言 > 详细

Python线程Event例子 (Boss要求等待员工生产杯子例子)

时间:2019-01-27 16:37:51      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:get   icc   event   for   tin   start   pytho   end   cups   

需求:Boss需要一个员工生产100个杯子,Boss一直等待员工生产完成之后方可
import threading
import logging
import time

logging.basicConfig(level=logging.INFO)

def worker(event:threading.Event, count=10):
    logging.info("I‘m working for U.")
    cups = []
    while True:
        logging.info(‘make 1‘)
        time.sleep(0.5)
        cups.append(1)
        if len(cups) >= count:
            event.set()
            break
    logging.info(‘I finished my job. cups={}‘.format(cups))

def boss(event:threading.Event):
    logging.info("I‘m boss, waiting for U.")
    event.wait()
    logging.info("Good Job")

Event = threading.Event()
w = threading.Thread(target=worker, args=(Event, 100))
b = threading.Thread(target=boss, args=(Event, ))
w.start()
b.start()

Python线程Event例子 (Boss要求等待员工生产杯子例子)

标签:get   icc   event   for   tin   start   pytho   end   cups   

原文地址:http://blog.51cto.com/windchasereric/2347053

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!