zoukankan      html  css  js  c++  java
  • Python 线程高级篇 threading 模块的使用

                                创建一个tread实例,给他传一个函数

     1 #!/usr/bin/python
     2 import  threading 
     3 from time  import *
     4 loops =[4,2]
     5 def  loop (nloop ,nsec):
     6    print ('start loop ',nloop,'at:', ctime())
     7    sleep(nsec)
     8    print ('loop', nloop ,'done at:', ctime())
     9 def main():
    10     print ('starting at:',ctime())
    11     threads=[]
    12     nloops=range(len(loops))
    13     for i in nloops:
    14         t=threading.Thread(target=loop,args=(i, loops[i]))
    15         threads.append(t)
    16     for i in nloops:
    17        threads[i].start()
    18     for i in nloops:
    19         threads[i].join()
    20     print ('all done at:',ctime())
    21 if  __name__=='__main__':
    22     main()
    23    

         创建一个tread实例给他传入一个可调用的类的实例

           

     1 #!/usr/bin/python
     2 import  threading 
     3 from time  import *
     4 loops=[4,2]
     5 class ThreadFunc(object):
     6     def  __init__(self,func,args,name=''):
     7         self.name=name
     8         self.func=func
     9         self.args=args
    10     def __call__(self):
    11          self.func(*self.args)
    12 def    loop(nloop ,nsec):
    13    print ('start loop ', 'at:',ctime())
    14    sleep(nsec)
    15    print ('loop',nloop,'done at',ctime())
    16 def main():
    17     print ('starting at:',ctime())
    18     threads=[]
    19     nloops=range(len(loops))
    20     for i in  nloops:
    21            t=threading.Thread(target=ThreadFunc(loop ,(i,loops[i]),loop.__name__))
    22 
    23            #t=threading.Thread(target=loop,args=(i, loops[i]))
    24            threads.append(t)
    25     for i  in nloops:
    26         threads[i].start()
    27     
    28     print  ('all done at :',ctime())
    29     
    30 if   __name__=='__main__':
    31      main()

                                  理解的代码:

     1 loops=[4,2]
     2 from time import *
     3 def    loop(nloop ,nsec):
     4    print ('start loop ', 'at:',ctime())
     5    sleep(nsec)
     6    print ('loop',nloop,'done at',ctime())
     7    
     8 class ThreadFunc(object):
     9 
    10     def  __init__(self,func,args,name=''):
    11         self.name=name
    12         self.func=func
    13         self.args=args
    14     def __call__(self):
    15         self.func(*self.args)  
    16       
    17 for i in  range(2):
    18     target=ThreadFunc(loop ,(i,loops[i]),loop.__name__)
    19     
    20     #target=(对象 ,一个参数 ,名字)
    21     print (target)
    22     print (target.name)
    23     print (target.func)
    24     #print (target.args)
    25     print (loop.__name__)
    26 target()
    27 print   (target())
    28 
    29 
    30 
    31 
    32    #t=threading.Thread(target=loop,args=(i,loops[i]))
    33   #t=threading.Thread(target=ThreadFunc(loop ,(i,loops[i]),loop.__name__))
    34     #t=threading.Thread(target=loop,args=(i, loops[i]))
    35     

                                关于__call__(self )的使用

    1 #!/usr/bin/poython
    2 class  ybl:
    3   def __init__(self ,name):
    4         self.name=name
    5   def __call__(self):
    6        print  (self.name)
    7 b=ybl('zx')
    8 print (b.name)
    9 b()
  • 相关阅读:
    修改Tarsphp节点线程数避免请求阻塞
    Docker删除所有容器
    清理mysql数据库binlog日志
    查看centos磁盘情况,查找大文件路径
    winform窗体的生命周期和事件加载顺序是什么?
    数据库自增ID用完了会怎么样?
    MPFIT for python
    Plplot中line width 问题
    剪切Postscript图片中的多余边框
    嵌入式下的深度学习 Sparkfun Edge with TensorFlow(一)Hello World
  • 原文地址:https://www.cnblogs.com/yubenliu/p/5718406.html
Copyright © 2011-2022 走看看