Reputation: 31
I found this code online and the predicted result was supposed to show the process names currently being processed, but when I run the code it only gives me the result and not the names. Im running Windows 7 Python 2.7.3.
Code:
import multiprocessing
def do_calculation(data):
return data * 2
def start_process():
print 'Starting', multiprocessing.current_process().name
if __name__ == '__main__':
inputs = list(range(10))
print 'Input :', inputs
builtin_outputs = map(do_calculation, inputs)
print 'Built-in:', builtin_outputs
pool_size = multiprocessing.cpu_count() * 2
pool = multiprocessing.Pool(processes=pool_size,
initializer=start_process,
)
pool_outputs = pool.map(do_calculation, inputs)
pool.close() # no more tasks
pool.join() # wrap up current tasks
print 'Pool :', pool_outputs
Expected Result:
$ python multiprocessing_pool.py
Input : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Built-in: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-11
Starting PoolWorker-12
Starting PoolWorker-13
Starting PoolWorker-14
Starting PoolWorker-15
Starting PoolWorker-16
Starting PoolWorker-1
Starting PoolWorker-2
Starting PoolWorker-3
Starting PoolWorker-4
Starting PoolWorker-5
Starting PoolWorker-8
Starting PoolWorker-9
Starting PoolWorker-6
Starting PoolWorker-10
Starting PoolWorker-7
Pool : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Upvotes: 3
Views: 687
Reputation: 21991
If I run the following code in a console and not IDLE (which I keep going back to after using PyCharm):
from multiprocessing import *
def main():
data = list(range(10))
print('Data:', data)
result = list(map(calculate, data))
print('Map: ', result)
pool = Pool(cpu_count() * 2, initialize)
result = pool.map(calculate, data, 1)
pool.close()
pool.join()
print('Pool:', result)
def calculate(number):
return number * 2
def initialize():
print('Starting', current_process().name)
if __name__ == '__main__':
main()
The following is printed to the console screen as you expected (with a random order for PoolWorkers):
C:\Users\schappell\Desktop>stack_overflow.py
Data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Map: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-1
Starting PoolWorker-13
Starting PoolWorker-2
Starting PoolWorker-3
Starting PoolWorker-8
Starting PoolWorker-5
Starting PoolWorker-14
Starting PoolWorker-16
Starting PoolWorker-15
Starting PoolWorker-12
Starting PoolWorker-6
Starting PoolWorker-9
Starting PoolWorker-4
Starting PoolWorker-10
Starting PoolWorker-7
Starting PoolWorker-11
Pool: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
C:\Users\schappell\Desktop>
As a side note, try running the following code instead (with and without time.sleep
commented out):
from multiprocessing import *
import time
def main():
data = list(range(10))
print('Data:', data)
print('Map: ', list(map(calculate, data)))
pool = Pool(cpu_count() * 2, initialize)
## time.sleep(1)
print('Pool:', pool.map(calculate, data, 1))
pool.close()
pool.join()
def calculate(number):
return number * 2
def initialize():
print('Starting', current_process().name)
if __name__ == '__main__':
main()
Without the sleep, you may get something like this (showing your pool has not been fully created yet):
C:\Users\schappell\Desktop>stack_overflow.py
Data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Map: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-2
Pool: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-1
Starting PoolWorker-10
Starting PoolWorker-9
Starting PoolWorker-6
Starting PoolWorker-14
Starting PoolWorker-13
Starting PoolWorker-5
Starting PoolWorker-4
Starting PoolWorker-3
Starting PoolWorker-8
Starting PoolWorker-11
Starting PoolWorker-12
Starting PoolWorker-16
Starting PoolWorker-7
Starting PoolWorker-15
C:\Users\schappell\Desktop>
Upvotes: 1