码迷,mamicode.com
首页 > 其他好文 > 详细

threading join用法

时间:2018-05-29 18:55:06      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:打印   def   size   white   hang   beijing   运行   import   style   

import threading #线程
import time

def Beijing(n):
print(‘Beijing time is start %s‘ % time.strftime(‘%Y-%m-%d %X‘, time.localtime()))
time.sleep(2)
print(‘Beijing time is over %s‘ % time.strftime(‘%Y-%m-%d %X‘, time.localtime()))


def Shanghai(n):
print(‘Shanghai time is start %s‘ %time.strftime(‘%Y-%m-%d %X‘,time.localtime()))
time.sleep(5)
print(‘Shanghai time is over %s‘ %time.strftime(‘%Y-%m-%d %X‘,time.localtime()))


if __name__ == ‘__main__‘:
t1 = threading.Thread(target=Beijing,args=(10,))
t1.start()

t2 = threading.Thread(target=Shanghai, args=(2,))
t2.start()

t1.join() #join 等待,t1.jion 就是必须等t1运行完成后,在接着运行后面的代码
print(‘Done‘)

运行的结果是:
  Beijing time is start 2018-05-29 17:37:18
  Shanghai time is start 2018-05-29 17:37:18
  Beijing time is over 2018-05-29 17:37:20
  Done
  Shanghai time is over 2018-05-29 17:37:23

Beijing Shanghai 同时执行开始时间 然后等待2秒,运行完beijing;接着打印Done;3秒后开始打印Shanghai结束时间



threading join用法

标签:打印   def   size   white   hang   beijing   运行   import   style   

原文地址:https://www.cnblogs.com/ajaxa/p/9106323.html

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