The python documentation for multiprocessing blames the lack of os.fork() for the problems in Windows. It may be applicable here.
See what happens when you import psyco. First, easy_install it:
C:Usershughdbrown>Python26scriptseasy_install.exe psyco
Searching for psyco
Best match: psyco 1.6
Adding psyco 1.6 to easy-install.pth file
Using c:python26libsite-packages
Processing dependencies for psyco
Finished processing dependencies for psyco
Add this to the top of your python script:
import psyco
psyco.full()
I get these results without:
serialrun took 1191.000 ms
parallelrun took 3738.000 ms
threadedrun took 2728.000 ms
I get these results with:
serialrun took 43.000 ms
parallelrun took 3650.000 ms
threadedrun took 265.000 ms
Parallel is still slow, but the others burn rubber.
Edit: also, try it with the multiprocessing pool. (This is my first time trying this and it is so fast, I figure I must be missing something.)
@print_timing
def parallelpoolrun(reps):
pool = multiprocessing.Pool(processes=4)
result = pool.apply_async(counter, (reps,))
Results:
C:UsershughdbrownDocumentspythonStackOverflow>python 1289813.py
serialrun took 57.000 ms
parallelrun took 3716.000 ms
parallelpoolrun took 128.000 ms
threadedrun took 58.000 ms
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…