Python multi-process and multi-threaded---(self-learning, persistent update)

Source: Internet
Author: User

First we know that the most obvious difference between a process and a thread is:

In a multi-process environment, each process has its own process data, and the process data is independent of each other. A process can have multiple threads, and multiple threads share the process's data.

#-*-Coding:utf-8-*-

Import OS

From NT Import getpid

Print ("Current ID:%d" %getpid ())

Output

Current id:17452

To create a process:

The creation process, the Linux fork, and the Linux environment under the C Programming fork function, for the parent process, the return is the child process PID, for the child process, the return is 0

From os import *

Print ("Current ID:%d" %getpid ())

PID = Fork()

If PID = = 0:

Print ("I am the child process:%d" %getpid ())

Else

Print ("I am parent process, my pid is%d, child process%d" %getpid (), PID)

Python is a platform-based language, so under Windows, Python's multiprocessing module enables multi-process programming.

Here's a simple example.

From multiprocessing import Process

From NT Import getpid

def my_proc():

Print (' I am the proc, pid(%d) ' %getpid ())

If __name__==' __main__ ':

Print (' Parent process%s. ' %getpid ())

p = Process (Target=my_proc)

Print (' Process would start. ')

P.start ()

P.join ()

Print (' Process end. ')

Python multi-process and multi-threaded---(self-learning, persistent update)

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.