Some questions about python multi-process and multi-thread

1. When there are 2 cpu, there is only one process. Does the process own 2 cpu resources?
2. Threads are under processes. If there are two processes with n threads under one cpu, process, then in theory, multithreading in this case cannot take advantage of multi-core. So to take advantage of multicore, there can only be one process?
3. Is the GIL of python one for each process or the only one for the whole?
4. Since there is GIL, it is said that multithreading has an advantage over "IO-intensive" tasks. My understanding is that the bottleneck of the task is reading and writing, although switching between multiple threads also requires cup resources, but because the computing task is not onerous, the switching between threads is smooth (inter-thread switching consumes less cpu resources than process switching), so multi-threading has an advantage over multi-processes in "IO-intensive" tasks. I don"t know if my understanding is wrong.

Please give me your advice. Thank you ~

Mar.20,2021

    The
  1. process has no correspondence with CPU. One process can take up two core by opening two threads.
  2. There is no correspondence between the
  3. process and the CPU, so there is no concept of having an exclusive CPU. Each of your two processes has two threads, which can take up 4 CPU.
  4. GIL one per process.

however, due to the existence of GIL in Python, Python can only have one thread actually running at a time in a process, so there are still many processes to take advantage of multicore advantage.

The phrase "multithreading has an advantage over IO-intensive tasks" is generally relative to "compute-intensive tasks", which is self-evident.
  1. Multithreading has an advantage over IO-intensive tasks. As for using single-process or multi-process to run IO-intensive tasks, there is little personal difference. After all, the bottleneck lies in the operating system and hardware.

  1. Yes, as long as your process has 2 + threads, it can fill the hole;
  2. is not sure what your definition of "exclusive" is. If you are forced to set processor correlation, you cannot take advantage of multi-core, but the advantage of multi-threading itself is still there. To take advantage of multiple cores, there may not be only one process, and one process per core can also be fully utilized.
  3. one per process;
  4. there are no errors. To open a sub-process, the cost of using the new GIL, is relatively high, and the communication is more troublesome. Open child threads, still under the control of the same GIL, but at a relatively low cost. Anyway, GIL will be released during IO, so there is no need for IO-intensive to pay so high a cost for a new GIL.

the cost comes from two aspects: handover cost and communication cost.

  • switching cost: process is higher than thread;
  • Communication cost: different processors are higher than the same processor.

in practice, we generally give each core a process, and then let each process manage its own thread, which can minimize two costs at the same time.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e46827-4c2c6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e46827-4c2c6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?