2016-09-15 5 views
1

나는 스레딩을 통해 두 파일에 병렬로 쓰려고합니다.두 파일에 평행하게 쓰기

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" 

여기 내 문제는 로그 파일이 스레드 2에서 생성되지 않습니다. 내가 뭘 잘못하고 있는지 알 수 있습니까?

+0

나는 당신의 명령을'ls'와'date'로 바꿨고 완벽하게 작동했습니다. 해볼 수 있니? 또한'subprocess'를 대신 사용해야 할 때'commands'를 사용하고 있다는 것에주의하십시오. –

+0

@JohnZwinck 일부는 I/O 연산을 제안하지만, 우리는 스레딩과 CPU 바운드 연산을 수행해야하며, 하위 프로세스로 가야합니다. –

+0

@AvinashRaj : 스레딩을 사용하거나 사용하지 않는 것에 대해서는 전혀 언급하지 않았습니다. 나는 '명령'을 사용하지 말라고 말했다. 더 이상 사용되지 않습니다. –

답변

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

해결책을 찾았습니다. 위의 코드는 저에게 효과적입니다.