许多线程同时写python的日志文件

use*_*946 9 python multithreading locking python-2.x

我正在编写脚本以同时从许多计算机中检索WMI信息,然后将这些信息写入文本文件中:
问题是所有线程在同一时间写入

f = open("results.txt", 'w+') ## to clean the results file before the start


def filesize(asset):  
    f = open("results.txt", 'a+')  
    c = wmi.WMI(asset)  
    wql = 'SELECT FileSize,Name FROM CIM_DataFile where (Drive="D:" OR Drive="E:") and Caption like "%file%"'  
    for item in c.query(wql):  
        print >> f, item.Name.split("\\")[2].strip().upper(), str(item.FileSize)  




class myThread (threading.Thread):  
    def __init__(self,name):  
        threading.Thread.__init__(self)  
        self.name = name  
    def run(self):  
        pythoncom.CoInitialize ()  
        print "Starting " + self.name       
        filesize(self.name)  
        print "Exiting " + self.name  



thread1 = myThread('10.24.2.31')  
thread2 = myThread('10.24.2.32')  
thread3 = myThread('10.24.2.33')  
thread4 = myThread('10.24.2.34')  
thread1.start()  
thread2.start()  
thread3.start()  
thread4.start()
Run Code Online (Sandbox Code Playgroud)

Mar*_*cny 24

您可以简单地创建自己的锁定机制,以确保只有一个线程写入文件.

import threading
lock = threading.Lock()

def write_to_file(f, text, file_size):
    lock.acquire() # thread blocks at this line until it can obtain lock

    # in this section, only one thread can be present at a time.
    print >> f, text, file_size

    lock.release()

def filesize(asset):  
    f = open("results.txt", 'a+')  
    c = wmi.WMI(asset)  
    wql = 'SELECT FileSize,Name FROM CIM_DataFile where (Drive="D:" OR Drive="E:") and Caption like "%file%"'  
    for item in c.query(wql):  
        write_to_file(f, item.Name.split("\\")[2].strip().upper(), str(item.FileSize))
Run Code Online (Sandbox Code Playgroud)

您可能需要考虑将锁定放在整个for循环周围,for item in c.query(wql):以允许每个线程在释放锁之前执行更大的工作.

  • 是的,当拥有锁的线程释放它时,等待的线程将获得锁。 (2认同)
  • @MartinKonecny 我正在将这种获取和释放方法用于 python 脚本,其中 4 个线程同时写入一个文件。它整齐地对齐任务并防止写入错误。我正在编写第二个(完全独立的脚本),它正在写入与第一个相同的文件。同时运行时,第一个脚本中实现的锁定和获取方法是否也阻止了第二个脚本(未实现)的同时访问?即,此方法是否会为当时尝试访问它的每个脚本锁定文件?...好奇,但文档不清楚。 (2认同)

Luk*_*raf 6

print不是线程安全的。使用logging模块代替(是):

import logging
import threading
import time


FORMAT = '[%(levelname)s] (%(threadName)-10s) %(message)s'

logging.basicConfig(level=logging.DEBUG,
                    format=FORMAT)

file_handler = logging.FileHandler('results.log')
file_handler.setFormatter(logging.Formatter(FORMAT))
logging.getLogger().addHandler(file_handler)


def worker():
    logging.info('Starting')
    time.sleep(2)
    logging.info('Exiting')


t1 = threading.Thread(target=worker)
t2 = threading.Thread(target=worker)

t1.start()
t2.start()
Run Code Online (Sandbox Code Playgroud)

输出(和的内容results.log):

[INFO] (Thread-1  ) Starting
[INFO] (Thread-2  ) Starting
[INFO] (Thread-1  ) Exiting
[INFO] (Thread-2  ) Exiting
Run Code Online (Sandbox Code Playgroud)

Thread-n您可以使用name关键字参数来设置自己的名称,而无需使用默认名称(),%(threadName)然后格式指令将使用:

t = threading.Thread(name="My worker thread", target=worker)
Run Code Online (Sandbox Code Playgroud)

(此示例改编自Doug Hellmann的出色文章中有关该threading模块的示例)