为什么multiprocess.Process不会在threading.Thread时写入文件?

时间:2012-08-01 22:31:20

标签: python multithreading concurrency multiprocessing

dotestsrl和dotestmt函数可以工作,它们可以创建和写入文件。 dotestmp函数运行速度很快,但不会创建也不会写入文件。我该怎么做才能使dotestmp执行与dotestmt相同的任务但使用multiprocessing.Process? (这是在双核,超线程机器上,运行Windows 7和Python 3.2.3;如果这很重要)

import sys, os, datetime, threading, multiprocessing

def dowrk(fp):
    with open(fp, mode="w") as f:
        for i in range(1000000):
            f.write(str(i))

def dotestsrl():
    fp = "C:\\Users\\user1\\Desktop\\"
    fn = "tst.txt"
    start = datetime.datetime.now()
    for fni in range(1, 5):
        dowrk(fp+str(fni)+fn)
    end = datetime.datetime.now()
    print("***DONE at: ", end - start)

def dotestmt():
    fp = "C:\\Users\\user1\\Desktop\\"
    fn = "tst.txt"
    lst = []
    start = datetime.datetime.now()
    for fni in range(1, 5):
        t = threading.Thread(target=dowrk, args=(fp+str(fni)+fn,))
        lst.append(t)
        t.start()
    for te in lst:
        te.join()
    end = datetime.datetime.now()
    print("***DONE at: ", end - start)

def dotestmp():
    fp = "C:\\Users\\user1\\Desktop\\"
    fn = "tst.txt"
    lsp = []
    start = datetime.datetime.now()
    for fni in range(1, 5):
        p = multiprocessing.Process(target=dowrk, args=(fp+str(fni)+fn,))
        lsp.append(p)
        p.start()
    for pe in lsp:
        pe.join()
    end = datetime.datetime.now()
    print("***DONE at: ", end - start)

dotestsrl() # ***DONE at:  0:00:08.029000
dotestmt() # ***DONE at:  0:00:07.697000
dotestmp() #  ***DONE at:  0:00:00.257000, but doesn't actually write files.

1 个答案:

答案 0 :(得分:0)

你可以尝试:

def dowrk_safe(*args, **kwargs):
    try:
        return dowrk(*args, **kwargs)
    except Exception:
        import sys
        import traceback
        traceback.print_exc(file=sys.stderr)

而不是dowrk()函数,看看是否有任何错误。