简体   繁体   English

并行写入两个文件python

[英]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 ? 这里我的问题是日志文件不是在线程2中创建的。我能知道我做错了什么吗?

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. 以上代码适合我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM