开发者

how to stop a thread in for loop in Python

开发者 https://www.devze.com 2022-12-07 18:10 出处:网络
How do I stop a for loop in a thread in Python 2.7 ? Is it possible to stop/continue script while using threading in a for loop ?

How do I stop a for loop in a thread in Python 2.7 ?

Is it possible to stop/continue script while using threading in a for loop ?

from threading import Thread
from Queue import Queue

q = Queue()
items=open('items.txt','r').read().splitlines()


def action():
    while True:
    开发者_开发问答    item = q.get()
        print item
        q.task_done()


if __name__=='__main__':
    for i in range(10):
        t = Thread(target=action)
        t.daemon=True
        t.start()
    for item in items:
        q.put(item)
    q.join()
0

精彩评论

暂无评论...
验证码 换一张
取 消