条件和定时器

条件

from threading import Condition,Thread

一个条件被创建之初,默认有一个False状态

False状态会影响wait 一直处于等待状态

notify制造钥匙(int数据类型)

def func(con,n):

  for i in range(10):

    con.acquire()

    con.wait() #等钥匙

    print('在第%s个循环里'%i)

    con.release()

con=Condition()

Thread(target=func,args=(con,))

while True:

  num = int(input('>>>'))

  con.acquire()

  con.notify(num)  #造钥匙

  con.release()

定时器

import time

from threading import Timer

def func():

  print('时间同步')

while True:

  t=Timer(5,func).start() #开启了一个线程

  time.sleep(5)

 

posted on 2020-05-14 22:16  bingzhang  阅读(141)  评论(0)    收藏  举报

导航