If I create a pool with 4 workers and set them to do some task (using pool.apply_async(..)
), I can access the name of each process from within using multiprocessing.current_process().name
, but how do I set the name from the parent process (this is mostly for logging)?
The name for a new process can be set via the “name” argument in the constructor to the multiprocessing. Process class when creating a new process.
Pool allows multiple jobs per process, which may make it easier to parallel your program. If you have a numbers jobs to run in parallel, you can make a Pool with number of processes the same number of as CPU cores and after that pass the list of the numbers jobs to pool. map.
Pool is generally used for heterogeneous tasks, whereas multiprocessing. Process is generally used for homogeneous tasks. The Pool is designed to execute heterogeneous tasks, that is tasks that do not resemble each other. For example, each task submitted to the process pool may be a different target function.
Python multiprocessing join The join method blocks the execution of the main process until the process whose join method is called terminates. Without the join method, the main process won't wait until the process gets terminated.
Process.name
is just a setter, you can freely assign to it.
The Pool takes an initializer
argument. This can be any callable, and it'll be called once when each subprocess starts up. You can point this to a function that sets the name
property of that process to whatever you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With