2

我想运行两个进程子作为

#!/usr/bin/env python
from multiprocessing import Process
import time

def method(namelog):
    filelog  = open(namelog,'w')
    while True:
        time.sleep(0.1)
        filelog.write('test log anything \n')
if __name__ == '__main__':
    p1 = Process(target=method, args=('log1.log',))
    print "start process1"
    p1.start()
    p2 = Process(target=method, args=('log2.log',))
    print "start process2"
    p2.start()

结果 :

start process1
start process2

program create 2 file log1.log and log2.log
but not save data

我创建了一个过程,但它不起作用帮助我!

4

1 回答 1

1

您必须关闭文件才能写入。请注意,我将模式open()从写入更改为附加。

试试这个:

from multiprocessing import Process
import time

def method(namelog):
    while True:
        with open(namelog,'a') as filelog:
            time.sleep(0.1)
            filelog.write('test log anything \n')
if __name__ == '__main__':
    p1 = Process(target=method, args=('log1.log',))
    print "start process1"
    p1.start()
    p2 = Process(target=method, args=('log2.log',))
    print "start process2"
    p2.start()
于 2013-11-15T04:48:56.227 回答