Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I name the processes in a multiprocessing.pool?

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)?

like image 802
Sveltely Avatar asked Nov 14 '14 04:11

Sveltely


People also ask

How do you name a process in Python multiprocessing?

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.

How do processes pools work in multiprocessing?

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.

What is the difference between pool and process in multiprocessing?

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.

What is process join () in Python?

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.


1 Answers

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.

like image 66
TkTech Avatar answered Oct 16 '22 02:10

TkTech