zoukankan      html  css  js  c++  java
  • Python 多线程 实例

    多线程实例

     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!
    运行结果
  • 相关阅读:
    mybatis 批量插入时候的一个注意点
    centos7 kubernetes单机安装
    debug 模式缓慢
    那些年,我们误解的 JavaScript 闭包
    maven仓库的配置
    闭包
    docker 搭建自己的github
    docker 搭建小型的node开发环境。
    jquery validate
    使用ueditor中的setContent() 时经常报innerHtml错误(笔记)
  • 原文地址:https://www.cnblogs.com/xsmile/p/10437313.html
Copyright © 2011-2022 走看看