I'm trying to learn to use Python's Multiprocessing module. As a first test, I thought I would run four 15 second processes at the same time. I wrote this module, which I called "multiPtest.py"::
import time
import timeit
import multiprocessing
def sleepyMe(napTime):
time.sleep(napTime)
print "Slept %d secs" % napTime
def tester(numTests):
#Launch 'numTests' processes using multiProcessing module
for _ in range(numTests):
p = multiprocessing.Process(target=sleepyMe(15))
p.start() #Launch an 'independent' process
#p.join() ##Results identical with or without join
def multiTester():
#Time running of 4 processes
totTime = timeit.Timer('tester(4)', setup = 'from multiPtest import tester').repeat(1,1)
print "Total = ", totTime[0]
However, when I run, I get these results:
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from multiPtest import *
>>> multiTester()
Slept 15 secs
Slept 15 secs
Slept 15 secs
Slept 15 secs
Total = 60.0739970207
I would have expected the Total time to be closer to 15 seconds, rather than 60. I know that I have 4 cores, because I looked at /proc/cpuinfo:
~/Projects/PythonProjects$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 60
model name : Intel(R) Core(TM) i7-4900MQ CPU @ 2.80GHz
stepping : 3
microcode : 0x17
cpu MHz : 800.000
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 0
cpu cores : 4
...
Why don't I see these 4 sleepers asleep at the same time? Shouldn't I have been able to create and launch new processes while others were asleep/busy? Am I misunderstanding something about multiprocessing, about Python's multiprocessing module or something else?
In the line
p = multiprocessing.Process(target=sleepyMe(15))
you actually already call sleepyMe
and use the result (None
) as the value for the target
parameter, thus waiting 15 seconds. Try
p = multiprocessing.Process(target=sleepyMe, args=(15, ))
and modify the function to join()
all subprocesses after the for loop, or it will return immediately and you'll end up with a total time close to zero..