python多线程写文件问题_许多线程在Python中同时写入日志文件

编程之家收集整理的这篇文章主要介绍了python多线程写文件问题_许多线程在Python中同时写入日志文件编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

I am writing a script to retrieve WMI info from many computers at the same time then write this info in a text file:

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()

The problem is that all threads writing at the same time.

解决方

You can simply create your own locking mechanism to ensure that only one thread is ever writing to a file.

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))

You may want to consider placing the lock around the entire for loop for item in c.query(wql): to allow each thread to do a larger chunk of work before releasing the lock.

总结

以上是编程之家为你收集整理的python多线程写文件问题_许多线程在Python中同时写入日志文件全部内容,希望文章能够帮你解决python多线程写文件问题_许多线程在Python中同时写入日志文件所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

猜你在找的Linux相关文章

概述 最近学习网络相关知识点,很多文章提到针对TCP time wait(后续简称TW)状态连接进行优化的参数tcp_tw_reuse和tcp_tw_recycle,并且不少文章提到了启用tcp_tw
转载请注明来源 'https://www.cnblogs.com/sogeisetsu/p/11407764.html' git reset 的HEAD和 hard 指的是从库恢复暂存区某一个文件,如
本文比较长,但讲的会比较细,如果只是想实现中文man的功能https://www.jianshu.com/p/262f6cc8f898
转载自git 中文官网 Git-工具-重置揭密
deactivate
对过去写的一些关于Linux的博客尝试做了一个整合的工作,我发现这个工作比我想象的要复杂,因为对之前写的文章多有不满,于是我尽力让文章变得通俗易懂且将所有引用的地方都标注出来,在整理的过程中,我有了一
增加 的启动项 问题描述 打开电脑,进入bios没有deepin启动项 解决办法 进入 ,选择 ,选择 ,增加启动项,名字写Linux,地址填 === 博客园 sogeisetsu 原创作品,转载请注
来自Stackoverflow
链接:http://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
微信公众号搜索 “ 程序精选 ” ,选择关注!
微信公众号搜 "程序精选"关注