使用Python向队列递归添加字符串

2024-09-27 22:32:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python读取一个包含一些单词的文件,将所有项目添加到队列中,然后使用doWork()方法执行操作a。我试图弄清楚,如何将在a操作中执行的结果添加回同一队列,以便再次对结果本身执行a。一旦q为空,循环就需要停止,并且它最终会停止,因为操作a可能返回结果,也可能不返回结果

我希望这有点道理P

concurrent = 20

def doWork():
    while True:
        word = q.get()
        lol = getStatus(word)
        q.task_done()


def getStatus(word):
    #results another word on which this operation has to be performed.



q = Queue(concurrent * 2)

for i in range(concurrent):
    t = Thread(target=doWork)
    t.daemon = True
    t.start()

try:
    for words in open('words.txt' ,'r'):
        q.put(words.strip())
    q.join()

except KeyboardInterrupt:
    sys.exit(1)

Tags: 文件项目方法intruefor队列def

热门问题