¿Cuál es la diferencia entre ThreadPool y Pool en el módulo de multiprocesamiento Python?
Cuál es la diferencia entreThreadPool
yPool
enmultiprocessing
módulo. Cuando pruebo mi código, esta es la principal diferencia que veo:
from multiprocessing import Pool
import os, time
print("hi outside of main()")
def hello(x):
print("inside hello()")
print("Proccess id: ", os.getpid())
time.sleep(3)
return x*x
if __name__ == "__main__":
p = Pool(5)
pool_output = p.map(hello, range(3))
print(pool_output)
Veo el siguiente resultado:
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
inside hello()
Proccess id: 13268
inside hello()
Proccess id: 11104
inside hello()
Proccess id: 13064
[0, 1, 4]
Con "ThreadPool":
from multiprocessing.pool import ThreadPool
import os, time
print("hi outside of main()")
def hello(x):
print("inside hello()")
print("Proccess id: ", os.getpid())
time.sleep(3)
return x*x
if __name__ == "__main__":
p = ThreadPool(5)
pool_output = p.map(hello, range(3))
print(pool_output)
Veo el siguiente resultado:
hi outside of main()
inside hello()
inside hello()
Proccess id: 15204
Proccess id: 15204
inside hello()
Proccess id: 15204
[0, 1, 4]
Mis preguntas son:
¿por qué se ejecuta el "exterior __principal __ ()" cada vez en elPool
?
multiprocessing.pool.ThreadPool
no genera nuevos procesos? ¿Solo crea nuevos hilos?
Si es así, ¿cuál es la diferencia entre usarmultiprocessing.pool.ThreadPool
en lugar de solothreading
¿módulo?
No veo ninguna documentación oficial paraThreadPool
en cualquier lugar, ¿alguien puede ayudarme donde pueda encontrarlo?