我在python中用多类和线程构建了一个代码,在我启动代码后,由于While True循环,代码保持运行,并在每个步骤中编写一个Report。我需要将一个外部函数链接到正在运行的代码以打印报告。
示例:
运行代码:
import threading as TH
def WriteTXT():
file = open('File.TXT','a')
file.write('Test_WriteTXT')
file.close()
runLoop()
def runLoop():
th.start()
th = TH.thread(target = WriteTXT)
th.start()
外部功能:
def report():
file = open('File.TXT','w')
Txt_file = file.read()
print(Txt_file)
如何调用报告函数并将其链接到运行代码以打印Txt_file?
您所要做的就是在代码末尾调用runLoop()
、WriteTXT()
和report()
函数。您还必须使用CCD_ 4线程";CCD_ 5";TH而不是"TH";ad";,并且在定义WriteTxt
函数时添加缺失的冒号(:
(。新代码可能如下所示:
import threading as TH
def WriteTXT():
file = open('File.TXT','a')
file.write('Test_WriteTXT')
file.close()
runLoop()
def runLoop():
th.start()
def report():
file = open('File.TXT','w')
Txt_file = file.read()
print(Txt_file)
WriteTxt()
th = TH.thred(target = WriteTXT)
th.start()
runLoop()