0
我想创建/打开并从线程写入文件。Python:无法打开并从线程写入文件
from threading import Thread
CONNECTION_PORT = 9191
def testl():
file = open("testfile.txt","w")
file.write("Hello World")
file.write("This is our new text file")
file.write("and this is another line.")
file.write("Why? Because we can.")
file.close()
def test():
t = Thread(target=testl)
# t.daemon = True
t.start()
test()
问题是,当我对测试函数的第二行(t.daemon = True)取消注释时,它停止工作。有什么办法让它在守护线程模式下工作?
我找不到任何解决方案在互联网上甚至与此有关。我知道这不是文件操作的最佳方式。
可能重复[Python守护进程线程退出时,父线程退出](https://stackoverflow.com/questions/21843916/python-daemon-thread-does-not-exit-when-parent-thread-exits ) – quamrana