zoukankan      html  css  js  c++  java
  • python class self thread join __init__.py

    self:python 的类的方法,与普通函数的明显区别,在类的方法必须有个额外的第一个参数 (self ),但在调用这个方法的时候不必为这个参数赋值显胜于隐 的引发)。Python的类的方法的这个特别的参数指代的是对象本身,而按照Python的惯例,它用self来表示。(当然我们也可以用其他任何名称来代替,只是规范和标准在那建议我们一致使用self)

    为何Python给self赋值而你不必给self赋值?

    例子说明:创建了一个类MyClass,实例化MyClass得到了MyObject这个对象,然后调用这个对象的方法MyObject.method(arg1,arg2) ,这个过程中,Python会自动转为Myclass.mehod(MyObject,arg1,arg2)。

    经典类:class Test:

          print “test”

    参数默认为“Object”,为父类,下面的例子指定父类是threading模块中的Thread类。

    #(myThread.py)

    class Mythread(threading.Thread):

      def __init__(self,p1,p2):

        self.p1=p1

        self.p2=p2

      def do:

        print self.p1,self.p2

    #call.py

    from myThread import MyThread

    t=Mythread(pp1,pp2)

    t.start

    t.join()

    关于join()Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.就是阻塞调用线程(主线程),执行被调用线程(如这里的t),直到t线程结束,或超时(若给timeout参数)。

    __init__.py很重要,讲很多py文件组织起来package。http://www.cnpythoner.com/post/2.html

    例:关于线程,类

    class Mythread(threading.Thread):
        def __init__(self,x,**kwargs):
            threading.Thread.__init__(self,kwargs=kwargs)                #调用父类的构造函数,用于 创建线程实例
            self.setDaemon(True)
            self.xx=x
            self.yy=x+500
            self.start()                            #Thread 类的函数 start(),开始线程的执行,run():定义线程功能的函数
        def run(self):
           # print self.xx ,type(self.xx), type( self.yy)
           threadi(self.xx,self.yy)
    k=0  
    c=0  
    d=0
    threads=[] 
    while k <=9000:
        try:
            thread=Mythread(k)                   #创建线程实例,并开始线程
            d+=1
            print d,'ddddddddddddddddddddddddddddddddddd'
        except:
            continue
        time.sleep(3)
        threads.append(thread)
        k+=500
       
    while len(threads):
        c+=1
        thread = threads.pop()
        if thread.isAlive():
            print c,'ccccccccccccccccccccc'
            thread.join()                               #等到线程函数结束,或在给了timeout参数时超时为止
      

  • 相关阅读:
    剑指 Offer 53
    Visual Studio Ultimate 2013
    手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
    DirectX的Vertex Buffer顶点缓冲的理解和应用 Shader必知必会
    Qt5.2中的android环境搭建
    JavaScript的基础学习篇
    九月十月百度,迅雷,华为,阿里巴巴最新校招笔试面试三十题(10.18)
    十月下旬腾讯,网易游戏,百度迅雷校园招聘笔试题集锦(第271-330题)
    九月十月百度人搜,阿里巴巴,腾讯华为笔试面试八十题(第331-410题)
    十月上旬百度,阿里巴巴,迅雷搜狗最新面试七十题(第201-270题)
  • 原文地址:https://www.cnblogs.com/xaf-dfg/p/3215119.html
Copyright © 2011-2022 走看看