Python: multi-process.

Reference: https://www.liaoxuefeng.com/wiki/1016959663602400/1017628290184064

 

Python program to achieve multi-process (multiprocessing)

 

Fork understand the linux system calls (wiki)

In the computer field, especially in Unix and Unix-like systems operating systems, fork (copy process) is a self-created stroke operation copies. It is usually a kernel implement a system call . Fork is one of the main ways to create processes on Unix-like operating system, or even the only way to history.

In a multitasking operating system, stroke (running) needed a way to create a new process, such as running other programs.

If the process needs to start another program's executable file, it needs to Fork to create a copy of itself. Then that is a copy of the " child process calls the" Exec system call, covering themselves with other programs: Stop the execution of the program before their own and perform other procedures.

 

When a process calls fork, it is considered to be the parent process , the newly created process is its child (the child). After the fork, the two processes is not only running the same program, and they resume execution (if they are called by the system) both processes not only run the same program, but they resume execution as though both had called the system call.. Then they can check the call's return value to determine its status: a parent or child process, and act accordingly.

 

Ruby multi-process handling module Process

Process Ruby provides a number of core modules and interface method corresponding unix.

fork[{block}] -> integer or nil

Create a child process. If a block is run in a child process. The child dies returns a status code 0 .

Fork call returns twice, once the parent process, which returns the process ID of the child; another is a child process returns nil.

Child process uses Kernel.exit! To exit.

Father process requires the use of Process.wait termination status code to collect the child process.

 

exec([env,] command...[,options])

Child process calls exec method, replacing the current process by running the command parameters.

 

pid -> integer

Return the current process id. Process.pid => 37415

 

ppid -> integer

Return the current process id of the parent process.

  • puts "#{Process.pid}" Process.fork { puts "child_id:#{Process.pid}\nfather_id:#{Process.ppid}"}

 

  • The first line of output current process id
  • The second line uses the fork block with a block in the sub-process to run, the output of its own child process id, and the id of the parent process, using ppid method, i.e., parent process id.

 

Multithreading Tutorial: https://www.runoob.com/ruby/ruby-multithreading.html


 

 

Python multiprocessing

OS module

Python's osmodule encapsulates the common system calls, including fork, you can easily create a child process in Python programs

import os

print('Process (%s) start...' % os.getpid())
# Only works on Unix/Linux/Mac:
pid = os.fork()
if pid == 0:
    print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
else:
    print('I (%s) just created a child process (%s).' % (os.getpid(), pid))

 

  • os.getpid () returns the current process id
  • os.getppid () id of the current process of the parent process.

Note: Windows is not forkcalled.

 

multiprocessing--Process-based parallelism

This module supports Win and Unix systems.

Process category

Creating a Process object, then call its start () method.

join ([timeout]) method can be passed before the end of the child process continues to run down, usually used for synchronization between processes.

 

 

 

 

Guess you like

Origin www.cnblogs.com/chentianwei/p/11881180.html