zoukankan      html  css  js  c++  java
  • python 线程间变量私有

    [root@yyjk ~/sbin/F5]#cat test4.py 
    import threading
    import time
    threads = []
    def fun9():
      x=100
      time.sleep(1)
      x= x+1;
      print x
      return x
      
      
    
    def fun10():
      x=200
      time.sleep(4)
      x=x+4
      print x
      return x;
    def fun11():
      x=300
      time.sleep(5)
      x=x+5
      print x;
      return x
      
    t9 = threading.Thread(target=fun9,name='fun9')
    threads.append(t9)
    t10 = threading.Thread(target=fun10,name='fun10')
    threads.append(t10)
    t11 = threading.Thread(target=fun11,name='fun11')
    threads.append(t11)
    
    for t in threads:
       t.setDaemon(True)
       t.start()
    for t in threads:
      t.join()
    
    [root@yyjk ~/sbin/F5]#vim test4.py
    [root@yyjk ~/sbin/F5]#cat test4.py 
    import threading
    import time
    threads = []
    def fun9():
      x=100
      time.sleep(1)
      x= x+1;
      print x
      return x
      
      
    
    def fun10():
      x=200
      time.sleep(7)
      x=x+4
      print x
      return x;
    def fun11():
      x=300
      time.sleep(5)
      x=x+5
      print x;
      return x
      
    t9 = threading.Thread(target=fun9,name='fun9')
    threads.append(t9)
    t10 = threading.Thread(target=fun10,name='fun10')
    threads.append(t10)
    t11 = threading.Thread(target=fun11,name='fun11')
    threads.append(t11)
    
    for t in threads:
       t.setDaemon(True)
       t.start()
    for t in threads:
      t.join()
    
    [root@yyjk ~/sbin/F5]#time python test4.py
    101
    305
    204
    
    real 0m7.051s
    user 0m0.014s
    sys 0m0.006s
    [root@yyjk ~/sbin/F5]#
  • 相关阅读:
    洛谷 P1284 三角形牧场WD
    luogu P3817 小A的糖果
    P3374 【模板】树状数组 1
    线程与threading模块
    socketserver模块
    python 粘包问题及解决方法
    python 网络编程
    类的进阶四 反射和内置方法
    python hashlib模块 logging模块 subprocess模块
    类的进阶三
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348399.html
Copyright © 2011-2022 走看看