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

Python 多线程 实例

时间:2019-02-26 15:40:11      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:targe   att   ide   target   opened   click   class   +=   17.1   

多线程实例

技术图片
 1 import threading
 2 import time
 3 
 4 
 5 def eat():
 6     eatTime = time.time()
 7     for i in range(30):
 8         print(‘count = {}‘.format(count))
 9         time.sleep(1)
10     print ‘eat something in {}‘.format(eatTime)
11     return eatTime
12 
13 def playGame():
14     playTime = time.time()
15 
16     for i in range(30):
17         global count
18         count += 1
19         time.sleep(1)
20     print ‘play Game in {}‘.format(playTime)
21     return playTime
22 count = 0
23 theadEat = threading.Thread(target=eat)
24 theatPlay = threading.Thread(target=playGame)
25 theadEat.start()
26 theatPlay.start()
27 eatT = theadEat.join()
28 playT = theatPlay.join()
29 
30 # eatT = eat()
31 # playT = playGame()
32 
33 print("over in {}!".format(time.time()))
多线程代码
技术图片
 1 count = 0
 2 count = 1
 3 count = 2
 4 count = 3
 5 count = 4
 6 count = 6
 7 count = 6
 8 count = 7
 9 count = 8
10 count = 9
11 count = 11
12 count = 11
13 count = 13
14 count = 14
15 count = 15
16 count = 16
17 count = 17
18 count = 18
19 count = 19
20 count = 20
21 count = 21
22 count = 22
23 count = 23
24 count = 24
25 count = 25
26 count = 26
27 count = 27
28 count = 28
29 count = 29
30 count = 30
31 play Game in 1551163617.11
32 eat something in 1551163617.11
33 over in 1551163647.13!
运行结果

 

Python 多线程 实例

标签:targe   att   ide   target   opened   click   class   +=   17.1   

原文地址:https://www.cnblogs.com/xsmile/p/10437313.html

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