多进程写入文件

from multiprocessing import Pool
from time import sleep
import time

def mycallback(x):
    with open('123.txt', 'a+') as f:
        f.writelines(str(x) + "\n")
        print x
        sleep(1)

def sayHi(num):
    return num

if __name__ == '__main__':
    e1 = time.time()
    pool = Pool(4)

    for i in range(10):
        pool.apply_async(sayHi, (i,), callback=mycallback)

    pool.close()
    pool.join()
    e2 = time.time()
    print float(e2 - e1)

你可能感兴趣的:(多进程写入文件)