Multithreading & Multi-process

Source: Internet
Author: User

One, multi-threaded

A conceptual process is an execution activity on a computer by a program. When you run a program, you start a process. Obviously, the program is Dead (static), and the process is alive (dynamic). Processes can be divided into system processes and user processes. The process used to complete various functions of the operating system is the system process, which is the operating system itself; The user process does not have to tell me more, all the processes you start are user processes. A process is the unit in which the operating system allocates resources. Its ideas are briefly described as follows: Under the management of the operating system, all running processes take turns using the CPU, each process is allowed to take up CPU time is very short (such as 10 milliseconds), so that the user does not feel that the CPU is in turn to serve multiple processes, as if all the processes are running continuously. But there is actually only one process that occupies the CPU at any one time.

Two, multi-process and multi-threaded differences

Multithreading uses a core of the CPU, suitable for IO-intensive multi-process use of multiple cores of the CPU, suitable for operation-intensive components Python provides a very useful multi-process package, multiprocessing, when we use, we only need to import the module. Multiprocessing supports sub-processes, communicates, shares data, performs different forms of synchronization, and provides components such as process,pipe, lock, etc.

Third, classroom field, method Supplement.

# p = multiprocessing. Process (target=, args=)
# target Specifies the function that needs to be executed when the process executes
# args is a parameter that needs to be passed to the function when the process executes
Note: args must be a tuple, especially if the function needs to pass in a parameter (1,)
# p stands for a multi-process,
# p.is_alive () determine if the process is alive
# P.run () Start process
# P.start () Start the process, he will automatically call the Run method, it is recommended to use the start
# P.join (timeout) waits for the child process to end or to a time-out
# p.terminate () forcing child process to exit
# The name of the p.name process
# PID of the P.PID process

Case one,

Import multiprocessing

Import time


def worker (args, interval):
Print ("Start worker {0}". Format (args))
Time.sleep (interval)
Print ("End worker {0}". Format (args))

def main ():
Print ("Start main")
P1 = multiprocessing. Process (Target=worker, args= (1, 1))
P2 = multiprocessing. Process (Target=worker, args= (2, 2))
P3 = multiprocessing. Process (Target=worker, args= (3, 3))
P1.start ()
P2.start ()
P3.start ()
Print ("End main")

if __name__ = = ' __main__ ':
Main ()

Case two,
Import multiprocessing

Import time


def worker (args, interval):
Print ("Start worker {0}". Format (args))
Time.sleep (interval)
Print ("End worker {0}". Format (args))

def main ():
Print ("Start main")
P1 = multiprocessing. Process (Target=worker, args= (1, 1))
P2 = multiprocessing. Process (Target=worker, args= (2, 2))
P3 = multiprocessing. Process (Target=worker, args= (3, 3))
P1.start ()
P1.join (timeout=0.5)
P2.start ()
P3.start ()
Print ("The number of the CPU is: {0}". Format (Multiprocessing.cpu_count ()))
For P in Multiprocessing.active_children ():
Print ("The name of the active children is: {0}, PID was: {1} is alive". Format (P.name, p.pid))
Print ("End main")

if __name__ = = ' __main__ ':
Main ()

Case three,
Import time

Import multiprocessing


def add1 (lock, value, number):
With Lock:
Print ("Start add1 number= {0}". Format (number))
For I in range (1, 5):
Number + = value
Time.sleep (0.3)
Print ("number = {0}". Format (number))

def add3 (lock, value, number):
Lock.acquire ()
Print ("Start add3 number= {0}". Format (number))
Try
For I in range (1, 5):
Number + = value
Time.sleep (0.3)
Print ("number = {0}". Format (number))
Except Exception as E:
Raise E
Finally
Lock.release ()
Pass

if __name__ = = ' __main__ ':
Print ("Start main")
Number = 0
Lock = multiprocessing. Lock ()
P1 = multiprocessing. Process (TARGET=ADD1, args= (lock, 1, number))
P3 = multiprocessing. Process (TARGET=ADD3, args= (lock, 3, number))
P1.start ()
P3.start ()
Print ("End main")

Case Four,
Import time

Import multiprocessing

From multiprocessing import Value, Array, Manager


def add1 (value, number):
Print ("Start add1 number= {0}". Format (Number.value))
For I in range (1, 5):
Number.value + = value
Print ("number = {0}". Format (Number.value))

def add3 (value, number):
Print ("Start add3 number= {0}". Format (Number.value))
Try
For I in range (1, 5):
Number.value + = value
Print ("number = {0}". Format (Number.value))
Except Exception as E:
Raise E

if __name__ = = ' __main__ ':
Print ("Start main")
Number = Value (' d ', 0)
P1 = multiprocessing. Process (TARGET=ADD1, args= (1, number))
P3 = multiprocessing. Process (TARGET=ADD3, args= (3, number))
P1.start ()
P3.start ()
Print ("End main")

Multithreading & Multi-process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.