メインスレッドが終了したときにデーモンスレッドはどうなりますか?彼らはどのように閉鎖されていますか?クリーンアップコードが確実に実行される方法はありますか?しばらく1メインスレッドが死亡した場合転がっ私は任意の一時ファイル(またはゾンビプロセス)を残していないことを確認したいデーモンスレッドはどのようにPythonで殺されますか(2.7)
import os
import subprocess
import threading
import glob
import datetime
def do_big_job(result='result.txt'):
tmpfile = result+'.tmp'
err = subprocess.call(['my_time_consuming_script', '-o', tmpfile])
if err == 0:
os.rename(tmpfile, result)
else:
os.remove(tmpfile) # process failed, clean up temp file
def main():
not_done = True
update = 0
while not_done:
# we can see if new data are available,
# and start a process to get/prep it
if new_data_available():
args = ('result{0:05}.txt'.format(update),)
th = threading.Thread(target=do_big_job, args=args)
update = update + 1
th.daemon = True
th.start()
# but we don't need/want to wait for that process
# to complete before continuing to iterate
currently_available = glob.glob('result*.txt')
# <snip>
# rest of loop just uses what is available,
# we don't want/need to block for updated data
# to meet other responsiveness requirements
:ここ
は、私が持っているような状況のスケッチですdo_big_job
スレッドの数(またはそれ以上)がまだ実行中ですが、メイン終了時に完了するのを待つことができないため、daemon=False
も設定できません。