taz taz - 1 year ago 77
Python Question

Writing Parallelly into two files python

I am just trying to write parallelly in to two file with the help of threading.

def dmesg (i):

cmd = 'dmesg'
print cmd
(status, cmd_out) = commands.getstatusoutput(cmd)
fil = open('dmesg_logs', 'w')

def dump (i):

cmd = 'lsmod'
print cmd
(status, cmd_out) = commands.getstatusoutput(cmd)
fil = open('logs', 'w')
if __name__ == "__main__":

t1 = threading.Thread(target = dmesg, args=(0,))
t2 = threading.Thread(target = dump, args=(0,))
while True :
"My own code"

Here my problem is logs file is not created in thread 2. Can i iknow what am doing wrong ?

taz taz
Answer Source
cmd = ['dmesg']
with open ('dmesg_log.txt', 'w') as out1:
    retun1 = subprocess.Popen(cmd, shell = True, stdout=out1)

Found the solution. Above code works for me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download