Skip to main content
 首页 » 编程设计

python之使用多线程将数据写入多个文件

2024年11月24日30wuhuacong

我想使用多个线程创建多个文件,并将数据(在执行某些操作后)从各自的线程附加到相应的文件。

我试过了,但是数据在线程之间变得困惑,正确的数据没有添加到相应的文件中。

import threading 
import time 
 
exitFlag = 0 
 
class myThread (threading.Thread): 
   def __init__(self, threadID, name, counter): 
      threading.Thread.__init__(self) 
      self.threadID = threadID 
      self.name = name 
      self.counter = counter 
   def run(self): 
      with open('file_'+count+'_logs.txt', 'a+') as result: 
            result.write("Starting " + self.name) 
            result.write("Exiting " + self.name) 
      print ("Starting " + self.name) 
      print_time(self.name, self.counter, 5) 
      print ("Exiting " + self.name) 
 
def print_time(threadName, delay, counter): 
   while counter: 
      if exitFlag: 
         threadName.exit() 
      time.sleep(delay) 
      print ("%s: %s" % (threadName, time.ctime(time.time()))) 
      counter -= 1 
 
myList = ['string0', 'string1', 'string2', 'string3'] 
 
if __name__ == "__main__": 
    count = 0 
    for data in myList: 
        count += 1 
        mythread = myThread(count, "Thread-" + str(count), count) 
        mythread.start() 
        mythread.join() 
 

我希望从 4 个线程创建 4 个文件,线程 1 的数据应该写入 file_1_logs.txt 等等...... 但是在写入数据时,有时所有数据都写入一个文件中。 如何将这些数据正确写入文件?

请您参考如下方法:

不要在线程中使用更高范围或全局变量。每个变量(您要修改的)都必须是线程的本地变量。

这意味着您需要将所有内容的初始值传递给 Thread 构造函数。对于像 print_time 这样的函数也是如此。函数完成其工作所需的一切都通过参数传递——或者将其转换为类方法。

考虑以下更改。请注意 MyThread 是如何完全独立的:

from threading import Thread 
from time import sleep 
from datetime import datetime 
 
class MyThread(Thread): 
    def __init__(self, threadID, name, delay, data): 
        Thread.__init__(self) 
        self.threadID = threadID 
        self.name = name 
        self.data = data 
        self.delay = delay 
        self.logfile = None 
 
    def run(self): 
        with open('file_%s_logs.txt' % self.threadID, 'a+') as logfile: 
            self.logfile = logfile 
            self.log("Starting") 
            self.print_time(5) 
            self.log("Exiting") 
            self.logfile = None 
 
    def print_time(self, repeat): 
        for c in range(repeat): 
            sleep(self.delay) 
            self.log(self.data) 
 
    def log(self, message): 
        now = datetime.now().isoformat() 
        formatted_line = "%s:%s:%s" % (now, self.name, message) 
        print(formatted_line) 
        if self.logfile: 
            self.logfile.write(formatted_line + '\n') 
 
 
if __name__ == "__main__": 
    myList = ['string0', 'string1', 'string2', 'string3'] 
    threads = [] 
 
    # spawn threads 
    for idx, data in enumerate(myList): 
        thread = MyThread(idx, "Thread-%s" % idx, idx, data) 
        threads.append(thread) 
        thread.start() 
 
    # wait for threads to finish 
    while True: 
        if any(thread.is_alive() for thread in threads): 
            sleep(0.1) 
        else: 
            print("All done.") 
            break