New thread blocks the main thread
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 ...
Can it be so? After new_thread.start () the main thread waits until it finishes, why is this happening? I have not provided new_thread.join () anywhere.
The daemon does not solve my problem because my problem is that the main thread stops immediately after starting a new thread, not because the main thread has finished executing.
source to share
As written, calling the constructor Thread
calls self.method2
instead of referencing it. Replace target=self.method2()
with target=self.method2
, and the threads will run in parallel.
Note that depending on what your threads are doing, CPU calculations may still be serialized due to the GIL .
source to share