如何解决如何在python中使用多线程将数据写入文本文件?
我想在python中使用多线程技术将数据附加到同一个文本文件中
这是我的代码_
import threading
my_list = ["a","b","c","d","e","f"]
def f1():
for item in my_list:
file = open("order_log.txt","a")
file.write(f"It is f1 {item}")
file.close()
def f2():
for item in my_list:
file = open("order_log.txt","a")
file.write(f"It is f2 {item}")
file.close()
def f3():
for item in my_list:
file = open("order_log.txt","a")
file.write(f"It is f3 {item}")
file.close()
def f4():
for item in my_list:
file = open("order_log.txt","a")
file.write(f"It is f4 {item}")
file.close()
t1 = threading.Thread(target=f1)
t2 = threading.Thread(target=f2)
t3 = threading.Thread(target=f3)
t4 = threading.Thread(target=f4)
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
但是当我执行程序时,它并没有写任何东西,任何人都可以告诉我我该怎么做。
解决方法
您需要设置为一次只有一个线程可以写入文件。这是通过让线程在文件上持有 Lock
来完成的。一旦线程处理完文件,它就可以释放 Lock
并且它会被等待它的线程之一占用。
这是一个如何使用锁和 Queue
的示例,因为大多数情况下,您要使用线程的原因是要对许多数据对象并行执行相同的操作。
import threading
import queue
import random
import time
tasks = queue.Queue()
output = open('output.txt','w')
output_lock = threading.Lock()
def worker(thread_number):
while not tasks.empty():
task = tasks.get()
# perform long calculation here...
time_to_spend = random.random()
time.sleep(time_to_spend)
result = task * task
# now we have result,want to write it
with output_lock: # this will block until the lock is available
print(thread_number,': square of',task,'is',result,'; took',time_to_spend,file=output)
tasks.task_done()
for i in range(100):
tasks.put(i)
for thread in range(8):
threading.Thread(target=worker,args=(thread,)).start()
print('waiting for tasks to complete')
tasks.join()
print('done')
我得到的输出是:
6 : square of 6 is 36 ; took 0.02233345201885739
7 : square of 7 is 49 ; took 0.0352967148552743
4 : square of 4 is 16 ; took 0.1043699083780637
7 : square of 9 is 81 ; took 0.2158108589024338
1 : square of 1 is 1 ; took 0.3330501408298937
4 : square of 10 is 100 ; took 0.3564233912485101
5 : square of 5 is 25 ; took 0.8496825534757959
0 : square of 0 is 0 ; took 0.8807306770021203
4 : square of 13 is 169 ; took 0.4420943872313102
7 : square of 11 is 121 ; took 0.6772180132068408
5 : square of 14 is 196 ; took 0.1101644871869385
6 : square of 8 is 64 ; took 0.944739067078435
3 : square of 3 is 9 ; took 0.9699315957506418
2 : square of 2 is 4 ; took 0.9903787965119304
3 : square of 20 is 400 ; took 0.029847547355710158
1 : square of 12 is 144 ; took 0.696648284935379
...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。