は突然スレッドを終了しようとすることはありません。代わりにWorkerThread
クラスにフラグ/シグナルを設定してから、停止させたいときはフラグを設定してスレッドを単独で終了させてください。
threading.Thread
をサブクラス化する方法についての誤解もあります。あなたがスレッドとしての機能を実行することを決定した場合、それは次のようになります。
thread = threading.Thread(target=my_func, args=(arg1, arg2...))
thread.start()
あなたが要求したときに、スレッドが停止したいので、まあ、あなたのケースでは、これはあなたのニーズに適合しません。だからthreading.Thread
のサブクラスを基本的に__init__
というコンストラクタのようなものにします。は、インスタンスが作成されるたびに実行されます。そしてすぐにstart()
スレッドをブロックしてjoin()
でブロックすると、forループの中にあるthreads.append(WorkerThread(running,file,count,len(fileTarget),"FindHeader"))
は、running
の最後までブロックされ、file
で終了し、次に別のfile
で動作するようになります。実際のスレッドは使用されていません。
あなたのrunning(fileopen,methodRun)
run()
に移動する必要があります。
class WorkerThread(threading.Thread):
def __init__(self,*args):
super(WorkerThread,self).__init__()
self.arg_0 = arg[0]
self.arg_1 = arg[1]
...
self.stop = False
def run(self):
# now self.arg_0 is fileopen, and self.arg_1 is methodRun
# move your running function here
#....long task of code are here...
Try to do self.stop check every small interval, e.g.
...
if self.stop:
return
...
for file in fileTarget:
new_thread = WorkerThread(file, count, len(fileTarget), "FindHeader"))
new_thread.start()
threads.append(new_thread)
# Stop these threads
for thread in threads:
thread.stop = True
がその実行を完了するためにスレッドをさせる、あなたはフラグの状態を変更することができ、すぐ以降、スレッド実行ブロックをロックブールフラグを設定メソッドをきれいに.... –
こんにちは、あなたの提案をありがとう、あなたはそれを処理するいくつかのコードを表示することができますか? 2〜4行のコードが実行されます。 – iamcoder
[Pythonでスレッドを削除する方法はありますか?](http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python) –