Python Foundation-------Process thread (ii)

Source: Internet
Author: User

  Process threads in Python (ii)

First, the "lock" in Python

1.GIL Lock (Global interpretation Lock)

Meaning:

The thread in Python is the native thread of the operating system, and the Python virtual machine uses a global interpreter lock (Global interpreter Lock) to mutex the use of the Python virtual machine. In order to support multithreading mechanism, a basic requirement is to implement the mutual exclusion of different threads to access shared resources, so the Gil is introduced.
GIL: After one thread has access to the interpreter, all other threads must wait for it to release the interpreter's access, even if the next instruction of those threads does not affect each other.
Before calling any Python C API, get the Gil first.
Gil disadvantage: Multiprocessor degenerate to single processor, Advantage: Avoid a lot of locking unlock operation.

Impact of 1.1GIL

No matter how many threads you have, how many CPUs do you have, Python will only allow a single thread to run at the same time while executing a process.
Therefore, Python is unable to utilize multi-core CPUs for multithreading.
In this way, Python is not as efficient as serial (no large switching) for computationally intensive tasks, but there is a significant increase in efficiency for IO-intensive tasks.

Io Intensive cases:

Import threading
Import time
def foo (n):


Time.sleep (N)
Print ("foo....%s"% n)

Print (Threading.activecount ())

def bar (N):

Time.sleep (N)
Print ("bar......%s"% n)

S=time.time ()
T1=threading. Thread (target=foo,args= (2,))
#t1. Setdaemon (True)
T1.start ()

T2=threading. Thread (target=bar,args= (5,))
#t2. Setdaemon (True)
T2.start ()

T1.join () # block main thread
T2.join ()

Print ("++++++", Threading.activecount ())
Print ("ending!")
Print ("Cost time:", Time.time ()-s)

Compute-intensive cases:

Import threading

Import time

def foo (n):
Ret=0
For I in range (n):
Ret+=i
Print (ret)


def bar (n): #计算密集型的任务指的是线程中存在大量的计算任务我们以阶乘和累加为例, through traditional serial execution functions and calculations.
Ret=1
For I in Range (1,n):
Ret*=i
Print (ret)

S=time.time ()
T1=threading. Thread (target=foo,args= (100000000,))
T1.start ()
T2=threading. Thread (target=bar,args= (100000,))
T2.start ()
T1.join ()
T2.join ()
Print ("Cost time:", Time.time ()-s)

Python Foundation-------Process thread (ii)

Related Article

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.