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参数时超时为止
      

  • 相关阅读:
    免费馅饼(HDU 1176 DP)
    搬寝室(HDU 1421 DP)
    FatMouse's Speed(HDU LIS)
    Bone Collector II(HDU 2639 DP)
    Palindrome(POJ 1159 DP)
    Proud Merchants(POJ 3466 01背包+排序)
    树的最大独立集
    Roads in the North(POJ 2631 DFS)
    Starship Troopers(HDU 1011 树形DP)
    Strategic game(POJ 1463 树形DP)
  • 原文地址:https://www.cnblogs.com/xaf-dfg/p/3215119.html
Copyright © 2011-2022 走看看