taz taz - 2 months ago 8
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')
fil.write(cmd_out)
fil.close()

def dump (i):

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

t1 = threading.Thread(target = dmesg, args=(0,))
t1.start()
t2 = threading.Thread(target = dump, args=(0,))
t2.start()
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
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.

Comments