在另一个函数中声明函数(self)时,'self'丢失了一些东西(self)

时间:2017-07-15 02:34:19

标签: python multithreading function pycharm self

请注意以下代码(Win10上的python 3.6,PyCharm),函数thread0(self)成功启动为线程,但thread1(self)似乎与thread0(self)的设置方式不同。 self.thread0很好,但self.thread1不是。 self中的self.thread1在其类函数中没有thread1,但它包含__init__()的所有内容。事实上,在PyCharm中,参数self甚至没有在行def thread1(self):中突出显示。我的理解是像foo(self)这样的语法会将foo()添加为self指向的类的成员。

由于我们在这里,我无法解释为什么启动thread0的try-catch块中的代码也失败了,也许它与线程的特定语法要求有关?

我有一种感觉,可能不建议使用这样的self嵌套。但是在我的实际代码中,我确实需要线程声明在除main()之外的新进程中,以便这些线程可以共享该进程的同一个python记录器。

import threading
import multiprocessing
from time import sleep


class exe0(multiprocessing.Process):
    def __init__(self):
        super().__init__()
        self.aaa = 111

        # working syntax for thread0
        t = threading.Thread(
            target=self.thread0,
            daemon=1,
        )
        t.start()

        try:
            # NOT working syntax
            t = threading.Thread(
                target=thread0,
                args=(self,),
                daemon=1,
            )
            t.start()
            sleep(1)
        except Exception as e:
            print(e)

    def thread0(self):
        print(type(self))

    def run(self):

        # working syntax for thread1
        def thread1(self):
            print(type(self))
            print(self.aaa)

        t = threading.Thread(
            target=thread1,
            args=(self,),
            daemon=1,
        )
        t.start()
        sleep(1)

        try:
            # NOT working syntax
            t = threading.Thread(
                target=self.thread1,
                daemon=1,
            )
            t.start()
            sleep(1)
        except Exception as e:
            print(e)


if __name__ == '__main__':
    multiprocessing.freeze_support()
    e = exe0()
    e.daemon = 1
    e.start()
    sleep(2)

# output:
'''
<class '__main__.exe0'>
name 'thread0' is not defined
<class '__mp_main__.exe0'>
111
'exe0' object has no attribute 'thread1'
'''

1 个答案:

答案 0 :(得分:1)

你忘了理解self是jussssst一个变量名,代表另一个东西,为了让你的代码正常工作,你只需要为你的变量选择另一个名字,看看:

  

重要编辑

您忘记在名为t4

的线程中定位该方法
import threading
import multiprocessing
from time import sleep


class exe0(multiprocessing.Process):
    def __init__(self):
        super().__init__()
        self.aaa = 111

        t1 = threading.Thread(
            target=self.thread0,
            daemon=1,
        )
        t1.start()

        try:
            t2 = threading.Thread(
                target=self.thread0, #here I removed the other parameter
                daemon=1,
            )
            t2.start()
            sleep(1)
        except Exception as e:
            print(e)

    def thread0(self):
        print(type(self))


    def run(self):
        def thread1(s): #here you can see the name doesn't matter
            print(type(s)) #here you can see the name doesn't matter
            print(s.aaa)


        t3 = threading.Thread(
            target=thread1(self), 
            daemon=1,
        )
        t3.start()
        sleep(1)

        try:
            t4 = threading.Thread(
                target=thread1(self), #here there is no need of the parameter
                daemon=1,
            )
            t4.start()
            sleep(1)
        except Exception as e:
            print(e)


multiprocessing.freeze_support()
e = exe0()
e.daemon = 1
e.start()
sleep(2)

现在您获得了 6个输出,例如:

<class 'exe0'>
<class 'exe0'>
<class 'exe0'>
111
<class 'exe0'>
111