用py编写:将程序内得到的三个线程的输出的三个变量a,b,c不断储存到创建的文档,文档按时间命名,储存的数据前带时间戳
时间: 2024-05-22 20:16:07 浏览: 14
import threading
import time
a = ""
b = ""
c = ""
def thread1():
global a
while True:
a = "Thread 1 output"
time.sleep(1)
def thread2():
global b
while True:
b = "Thread 2 output"
time.sleep(2)
def thread3():
global c
while True:
c = "Thread 3 output"
time.sleep(3)
def write_to_file():
while True:
current_time = time.strftime("%Y%m%d-%H%M%S")
filename = current_time + ".txt"
with open(filename, "w") as f:
f.write(current_time + " " + a + "\n")
f.write(current_time + " " + b + "\n")
f.write(current_time + " " + c + "\n")
time.sleep(5)
t1 = threading.Thread(target=thread1)
t2 = threading.Thread(target=thread2)
t3 = threading.Thread(target=thread3)
t4 = threading.Thread(target=write_to_file)
t1.start()
t2.start()
t3.start()
t4.start()