小编use*_*946的帖子

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

我正在编写脚本以同时从许多计算机中检索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') …
Run Code Online (Sandbox Code Playgroud)

python multithreading locking python-2.x

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

locking ×1

multithreading ×1

python ×1

python-2.x ×1