线程之间的执行顺序是无序的
import threading
import time
def rask():
time.sleep(1)
print(threading.current_thread())
pass
if __name__ == '__main__':
for item in range(5):
rask_threading = threading.Thread(target=rask)
rask_threading.start()
pass
<Thread(Thread-2, started 18756)>
<Thread(Thread-1, started 20816)>
<Thread(Thread-4, started 18496)>
<Thread(Thread-3, started 7556)>
<Thread(Thread-5, started 20920)>
主线程会等待子线程执行完结果
- 如果子线程是一个死循环的话?
- 让子线程守护主线程demaon(就可以结束死循环)
import threading
import time
def rask():
while True:
print("正在执行中.....")
time.sleep(0.2)
pass
pass
if __name__ == '__main__':
# 方法一:
# rask_threading = threading.Thread(target=rask, daemon=True)
rask_threading = threading.Thread(target=rask)
# 方法二
rask_threading.setDaemon(True)
rask_threading.start()
time.sleep(1)
print("over")