Good day!

Interested in a little strange, probably for many, the obvious question of multiprocessing. There is an application on CPython that creates and manages several instances of classes derived from multiprocessing.Process .

Suppose that a situation has arisen, as in this diagram. Four processes are performed in parallel at a given time. The fifth, sixth, seventh are waiting for their turn. Everything is peaceful, quiet and then EXTREMELY the process "Process # 2" decides to take a short nap by passing the command time.sleep (1) . So Process # 2 pauses execution for 1 second.

Question. What will happen during sleep? Process # 2 : Core # 2 will be busy with one of the queue processes or stupidly idle, waiting for the completion of sleep Process # 2 ?

UPD : sample code

  • Thread during sleep is blocked. But I don’t know for sure if the percents will start at this time another one that is waiting for a thread or not. - Daniyar Supiev
  • one
    @uzumaxy, where did you get this same Queue from? Give an example code . - avp
  • one
    If I correctly understood multiprocessing. Process, then the other one, just Process # 2 will be temporarily excluded from the list of those ready to be executed. Therefore, 4 cores will be shared between the remaining processes. - KoVadim
  • one
    Of course, I will give the most distant comment, but isn't it easier to check? - etki
  • Queue - a queue of processes for execution. The implementation of the processor's time distribution system in each OS may be different, but it is possible to perform more processes at a time than the percent supports. impossible, as I understand it (without emulation; using additional processors, including graphics and other Labuda). Therefore, to build a queue for execution. She is the Queue that is shown in the diagram. This is a bit rude and inaccurate, even if we consider the situation only in the context of the processes, depriving the flow of attention. - uzumaxy

1 answer 1

@uzumaxy , do you mean runqueue in OS ???

As far as I understand, you are running (in the example) 8 independent processes without any Python managers.

If so, then do not worry. The operating systems are fairly well written and do not allow idle CPU if there are processes ready to use them.

Moreover, if there are many more processes than the available processors, the processor time will be allocated to all these processes in turn (on a "fair" basis). In Linux, this redistribution will take place 100 times per second if there is no other (except computational) work on the system.

  • If runqueue is a queue of active processes, then yes - it is about her speech. But, as far as I know, when creating a new process from under the Python VM, it is still weighed in with a bunch of all sorts of trinkets like your own GC. That is why I ask. That's because in the tags "python". The OS will not allow the process to stand idle, but somehow it communicates with the processes. And if the latter decided to self-terminate, he should inform the process planner about this. So I don’t know what Python says when it encounters a "time.sleep" call - uzumaxy
  • In other words. What will be the process created by Python to inform the OS after the sleep command: "Put another process in my place, I have done my job" or "My processor time has not yet come out! - uzumaxy
  • As far as I know, there is simply no real implementation of threads (threads) in python. Therefore, it is not worth talking about multiprogramming on threads. Judging by the explanations to the Joining zombie processes in * nix, the interpreter simply does fork() and executes the code in the new process. Accordingly, sleep() will be the same sleep () as in a normal C program. And such "dialogues" with the core "Put another process in my place, ...." in reality just does not happen. - avp
  • one
    In python sauce, it seems to have found the implementation of the 'time.sleep' method: 'Modules / timemodule.c', the time_sleep function (but the main logic is in floatsleep). As you have stated, the platform-specific process interruption method is called there, for example for win32: Sleep () Thanks for the help. - uzumaxy
  • one
    @uzumaxy, as far as I know it was done on purpose. Besides this implementation, there were others, but they somehow had a bad effect on the performance of single-threaded applications, so Guido refused to accept them. This was his condition - such a multithreading implementation that does not degrade the current performance of single-threaded applications. - BOPOH