在Python中多个线程同时写入日志文件
我正在编写一个脚本,以便同时从多台计算机检索WMI信息,然后将此信息写入文本文件:在Python中多个线程同时写入日志文件,python,multithreading,locking,python-2.x,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_DataF
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()
问题是所有线程同时写入。您只需创建自己的锁定机制,以确保只有一个线程写入文件
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))
您可能想考虑在C.Queq(WQL):中为项目放置整个循环for <代码>,以允许每个线程在释放锁之前执行更大的工作。
打印
不是线程安全的。改为使用模块(即:
输出(以及结果.log的内容)
:
您可以使用name
关键字参数设置自己的名称,而不是使用默认名称(Thread-n
),然后%(threadName)
格式化指令将使用该参数:
t = threading.Thread(name="My worker thread", target=worker)
(此示例改编自的示例)对于另一种解决方案,请使用
池来计算数据,并将其返回到父进程。然后,该父级将所有数据写入一个文件。因为一次只有一个进程写入文件,所以不需要额外的锁定
注意:下面使用的是进程池,而不是线程池。这使得代码比使用threading
模块组合起来简单得多。(有一个ThreadPool
对象,但没有文档记录。)
来源
在results.txt中输出
如果一个线程试图在文件被锁定到另一个线程的情况下将\u写入\u文件,那么两个线程是否仍将轮到他们写入?是的,当拥有锁的线程释放它时,等待的线程将获得锁。@MartinKonecny我对一个python脚本使用此获取和释放方法,其中4个线程同时写入一个文件。它整齐地排列任务并防止书写错误。我正在编写第二个(完全独立的脚本),该脚本与第一个脚本写入同一个文件。当同时运行时,在第一个脚本中实现的lock and acquire方法是否也会阻止第二个脚本同时访问(在未实现的情况下)?也就是说,对于当时试图访问该文件的每个脚本,该方法是否都会锁定该文件。。。奇怪的是,文档不清楚。不,这种锁定方法只适用于在同一脚本中运行的线程。为什么有些人将与lock:#operation
[INFO] (Thread-1 ) Starting
[INFO] (Thread-2 ) Starting
[INFO] (Thread-1 ) Exiting
[INFO] (Thread-2 ) Exiting
t = threading.Thread(name="My worker thread", target=worker)
import glob, os, time
from multiprocessing import Pool
def filesize(path):
time.sleep(0.1)
return (path, os.path.getsize(path))
paths = glob.glob('*.py')
pool = Pool() # default: proc per CPU
with open("results.txt", 'w+') as dataf:
for (apath, asize) in pool.imap_unordered(
filesize, paths,
):
print >>dataf, apath,asize
zwrap.py 122
usercustomize.py 38
tpending.py 2345
msimple4.py 385
parse2.py 499