Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 新线程阻塞主线程_Python_Multithreading - Fatal编程技术网

Python 新线程阻塞主线程

Python 新线程阻塞主线程,python,multithreading,Python,Multithreading,有可能这样做吗? 在新的_thread.start()之后,主线程会等待它完成,为什么会发生这种情况?我没有在任何地方提供新的线程join() 守护进程没有解决我的问题,因为我的问题是主线程在新线程启动后立即停止,而不是因为主线程执行已结束。IIRC,这是因为程序在所有非守护进程线程完成执行后才退出。如果改用守护进程线程,它应该可以解决这个问题。此答案提供了有关守护进程线程的更多详细信息: 如前所述,对线程构造函数的调用是调用self.method2,而不是引用它。用target=self.me

有可能这样做吗? 在新的_thread.start()之后,主线程会等待它完成,为什么会发生这种情况?我没有在任何地方提供新的线程join()


守护进程没有解决我的问题,因为我的问题是主线程在新线程启动后立即停止,而不是因为主线程执行已结束。

IIRC,这是因为程序在所有非守护进程线程完成执行后才退出。如果改用守护进程线程,它应该可以解决这个问题。此答案提供了有关守护进程线程的更多详细信息:


如前所述,对
线程
构造函数的调用是调用
self.method2
,而不是引用它。用
target=self.method2
替换
target=self.method2()
,线程将并行运行


请注意,根据您的线程所做的工作,CPU计算可能仍然会由于以下原因而序列化。

谢谢!有时候我自己就是找不到这些愚蠢的错误。
from threading import Thread
class MyClass:
    #...
    def method2(self):
        while True:
            try:
                hashes = self.target.bssid.replace(':','') + '.pixie'
                text = open(hashes).read().splitlines()
            except IOError:
                time.sleep(5)
                continue
        # function goes on ...

    def method1(self):
        new_thread = Thread(target=self.method2())
        new_thread.setDaemon(True)
        new_thread.start()  # Main thread will stop there, wait until method 2 

        print "Its continues!" # wont show =(
        # function goes on ...