开发者

how to stop a thread in for loop in Python

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜