新手求问,刚学习python,发现在两个线程或两个进程开启后(都有一个while循环),必须要第一个执行完成,第二个才会执行,求问为什么会这样
import threading
import time
class Test:
def run(self):
threading.Thread(target=self.func1()).start()
threading.Thread(target=self.func2()).start()
def func1(self):
count = 0
while count < 3:
print("thread1")
count += 1
time.sleep(2)
def func2(self):
count = 0
while count < 3:
print("thread2")
count += 1
time.sleep(1)
if __name__ == "__main__":
Test().run()
import multiprocessing
import time
class Test:
def run(self):
multiprocessing.Process(target=self.func1()).start()
multiprocessing.Process(target=self.func2()).start()
def func1(self):
count = 0
while count < 3:
print("thread1")
count += 1
time.sleep(2)
def func2(self):
count = 0
while count < 3:
print("thread2")
count += 1
time.sleep(1)
if __name__ == "__main__":
Test().run()
执行结果:
thread1
thread1
thread1
thread2
thread2
thread2