Python:线程只能启动一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36937667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: threads can only be started once
提问by Sonius
I want to do threading in python. I have 100 words and want to put them in 6 different links. If one of the links is ready, I want that the link can get the new word. This while the other threads have still the first word in work. My complete program should be allowed to do more code first when the 100 keywords are done. I have the following code:
我想在python中进行线程处理。我有 100 个单词,想把它们放在 6 个不同的链接中。如果其中一个链接已准备好,我希望该链接可以获取新词。这同时其他线程仍然在工作中的第一个单词。完成100个关键字后,我的完整程序应该可以先做更多的代码。我有以下代码:
threads = []
def getresults(seed):
for link in links:
t = threading.Thread(target=getLinkResult, args = (suggestengine, seed))
threads.append(t)
for thread in threads:
thread.start()
for seed in tqdm:
getresults(seed + a)
getresults(seed + b)
for thread in threads:
thread.join()
#code that should happen after
I get an error at the moment: threads can only be started once
我现在收到一个错误:线程只能启动一次
回答by Games Brainiac
You are calling getresults
twice, and both times, they reference the same global threads
list. This means, that when you call getresults
for the first time, threads are started.
您调用了getresults
两次,并且两次都引用了相同的全局threads
列表。这意味着,当您getresults
第一次调用时,线程会启动。
When you call them for the second time, the previous threads that are already running, have the .start()
method invoked again.
当您第二次调用它们时,之前已经在运行的线程会.start()
再次调用该方法。
You should start threads in the getresults
as local threads, and then append them to the global threads
list.
您应该在getresults
本地线程中启动线程,然后将它们附加到全局threads
列表中。
Although you can do the following:
虽然您可以执行以下操作:
for thread in threads:
if not thread.is_alive():
thread.start()
it does not solve the problem as one or more threads might've already ended and therefore be started again, and would therefore cause the same error.
它不能解决问题,因为一个或多个线程可能已经结束并因此重新启动,因此会导致相同的错误。
回答by ventik
You should start only new threads in your getresults
您应该只在 getresults 中启动新线程
threads = []
def getresults(seed):
local_threads = []
for link in links:
t = threading.Thread(target=getLinkResult, args = (suggestengine, seed))
local_threads.append(t)
threads.append(t)
for thread in local_threads:
thread.start()
for seed in tqdm:
getresults(seed + a)
getresults(seed + b)
for thread in threads:
thread.join()
回答by Arthur Havlicek
The error is explicit. You start your threads twice, while you shouldn't.
错误是明确的。你开始你的线程两次,而你不应该。
getresults(seed + a)
getresults(seed + b)
When you sequence these calls you start twice the loop of threads. To properly do what you want to do, you to make a thread pool and a task queue. Basically, you need a second list of words to process and a mutex. Each thread will lock the mutex, read and dequeue a word, then unlock and process the word.
当您对这些调用进行排序时,您将启动两次线程循环。为了正确地做你想做的事,你需要创建一个线程池和一个任务队列。基本上,您需要第二个要处理的单词列表和一个互斥锁。每个线程都会锁定互斥锁,读取并出列一个字,然后解锁并处理该字。