0

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.
4

1 回答 1

0

你可以试试:

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

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

于 2012-08-01T22:48:20.820 回答