为什么for in 循环后还会执行调用的函数

当第i=5的时候,按道理说就要跳出for in 循环,也就是总共执行5次rest函数,为什么我这个的确能并发执行3次,总共执行完15次rest函数。想不明白了,求解?

import queue
import threading, time

def rest(que):
    while not que.empty():
        q_name = que.get()
        time.sleep(5)
        print(q_name + ' > wake up.')


q = queue.Queue()
for i in range(1, 16):
    q.put('dev %d' % i)

max_thread = 5
ts = []
for i in range(max_thread):
    t = threading.Thread(target=rest, args=(q,))
    t.start()
    ts.append(t)

这个就是执行了5次rest函数,你在while上边打印个东西看看。它能执行完全部15次是在while里边执行的。

img


我这里打印了ok就只有5次懂了吗。