I have a question about python's multiprocessing module, when I'm trying to find a pin why is 1 process faster than 10 or 15 processes, I thought the more processes you got running the faster you will get your results, but for some reason that's not the case can someone example why the script takes a longer time when the for loop number is greater than 1. # - Code 1 #!/usr/bin/env python # # # from multiprocessing import Process from random import choice from subprocess import * from time import sleep
def PinGuess(pin,nums):
global Found,Invalid
while not Found:
Numbers = xrange(nums)
current_pin = int(choice(Numbers))
if current_pin not in Invalid:
call(['clear'])
print 'Trying: {}'.format(current_pin)
if current_pin == pin: Found = True
else: Invalid.append(current_pin)
print len(Invalid)
if __name__ == '__main__':
Found = False
Invalid = []
Pin = 123
for i in range(1):
Process(target=PinGuess, args=(Pin,300)).start()
# - Output 1
Trying: 123
104
real 0m0.904s
user 0m0.316s
sys 0m0.440s
# - Code 2
#!/usr/bin/env python
#
#
#
from multiprocessing import Process
from random import choice
from subprocess import *
from time import sleep
def PinGuess(pin,nums):
global Found,Invalid
while not Found:
Numbers = xrange(nums)
current_pin = int(choice(Numbers))
if current_pin not in Invalid:
call(['clear'])
print 'Trying: {}'.format(current_pin)
if current_pin == pin: Found = True
else: Invalid.append(current_pin)
print len(Invalid)
if __name__ == '__main__':
Found = False
Invalid = []
Pin = 123
for i in range(10):
Process(target=PinGuess, args=(Pin,300)).start()
# - Output 2
Trying: 123
296
real 0m10.530s
user 0m3.652s
sys 0m8.328s