您可以简单地创建自己的锁定机制来确保只有一个线程正在写入文件。
import threadinglock = 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))您可能需要考虑在整个for循环中放置锁,
for item in c.query(wql):以允许每个线程在释放锁之前做较大的工作。



