You are right about Python If you know something about multithreading . So you are right python Multithreading in single cpu Multithreading and multithreading in a sense cpu There is a fundamental difference between multithreading on , If you are right about Python I want to know more about multithreading , You can browse through our articles .

Python Multithreading is single cpu Multithreading in a sense , It's more like cpu There is a fundamental difference between multithreading on .

single cpu Multithreading : Concurrent

many cpu Multithreading : Parallelism contains concurrency inside

stay Python In multithreading , There is one called Global Interpreter Lock(GIL) Things that are , Global interpreter lock . Its function is to have only one thread at a time for python Object to operate on .Python Various mechanisms have been provided for multithreading synchronization , Why do you have to fix this GIL Well ? This is because programmer controlled synchronization is for variables that are visible in each program , and GIL Synchronized are invisible variables in the interpreter background , For example, the reference count maintained for garbage collection . without GIL, It is possible to release the same object twice due to thread switching .

therefore , Any one of them CPython If the thread wants to execute , You have to get this first GIL. consequence ? Is in the CPython in , Essentially, there is almost no thread parallelism , No matter how many threads you open , At the same time, only gain GIL That thread of can execute . Why say almost , That's because it's available to python Of C In the library , There are still solutions , such as

This code is sleep Code for , In execution sleep Before , Release through a macro GIL, And then at the end of sleep, before executing other code, get GIL. Other operations , such as IO, There will be similar operations , This makes it possible for IO Intensive program , Or use C Library to calculate the program , It can still be avoided to a large extent GIL To achieve the effect of thread parallelism . But for pure python Code program ,GIL I'm afraid I can't escape .

There's another problem , Namely GIL How to release , We see in the python/C API Macro is provided in to release , So for ordinary python Sentence? ? The interpreter will execute a hundred python Force release after code GIL, This allows other threads to execute .

Last but not least , This is this. GIL The problem is interpreter related , Not language related . In other words, it's just for python An implementation of a language interpreter , It's not the nature of language itself . in fact ,GIL It's a very coarse-grained lock of the interpreter , We can use more fine-grained locks to increase parallelism , and Gindo I wrote about one patch To cancel GIL, However, it seems that the final result is that fine-grained locking causes the performance of single threaded programs to decrease by two times , So in the end, we decided to give priority to the performance of single threaded programs , Keep on GIL. however python The other two branches of ,Jython and IronPython, But none of them GIL The problem of , In this way, we can realize the parallel of threads .

summary :

Usually locking also has 2 Different granularity locks :

fine-grained( The so-called fine-grained ), So programmers need to add , Unlock to ensure thread safety

coarse-grained( So called coarse grain size ), Then the language level itself maintains a global locking mechanism , To ensure thread safety

Python Multithreading From the language level, it maintains a global locking mechanism , To ensure thread safety ; and java, Jython It is fine-grained .

So that is to say , because gil The limitation of ,python The language itself is not capable of parallel programming , But you can do concurrent programming ; and java There is no gil Limitations in a sense , therefore java from java7 It's starting to move up parallel .

< turn >Python Multithreaded single cpu And cpu Multithreading on the difference between more related articles

  1. python The difference between single underline and double underline

    python The difference between single underline and double underline Python Use underscores as variable prefixes and suffixes to specify special variables . _xxx Out-of-service 'from moduleimport *' Import __xxx__ System definition name __ ...

  2. python The difference between single underline and double underline ( Private and protected )

    Python Use underscores as variable prefixes and suffixes to specify special variables . _xxx Out-of-service 'from moduleimport *' Import __xxx__ System definition name __xxx Private variable name in class Core style : Avoid underscores ...

  3. python Why multithreading can't take advantage of multicore cpu

    GIL And Python Thread entanglement GIL What is it? ? It's good for our python What is the impact of the program ? Let's start with a question . Run the following paragraph python Program ,CPU What's the occupancy rate ? # Please don't work ...

  4. python GIL Global lock , Multicore cpu How about the performance of multithreading under the ?

    python GIL Global lock , Multicore cpu How about the performance of multithreading under the ?GIL Full name Global Interpreter Lock GIL What is it? ? The first thing to be clear is GIL Not at all Python Characteristics of , It's about achieving ...

  5. [ Deep learning ] Pytorch( 3、 ... and )—— many / single GPU、CPU, Training preservation 、 Loading model parameters

    [ Deep learning ] Pytorch( 3、 ... and )-- many / single GPU.CPU, Training preservation . Load prediction model problem In the last practical study , Met in many / Single GPU.GPU And CPU Training in different environments . The problem of loading the usage model , If you save ...

  6. Python The tenth day of the road , senior (2)- Multithreading , Multi process , coroutines

    Threads threading threading Module object describe Thread Represents the execution object of a thread Lock Lock primitive objects RLock Reentrant lock object , So that a single thread can acquire the lock that has been acquired again ( Recursive locking ) Cond ...

  7. JAVA The journey ( fourteen )—— The lock of static synchronization function is class object , Multithreaded singleton design patterns , Deadlock , Thread communication and the security risks brought by communication , Wait for the wake-up mechanism

    JAVA The journey ( fourteen )-- The lock of static synchronization function is class object , Multithreaded singleton design patterns , Deadlock , Thread communication and the security risks brought by communication , Wait for the wake-up mechanism JAVA The journey , You all the way , come on. ! One . The lock of static synchronization function is clas ...

  8. [svc][cpu][jk]cpu The core view and what is cpu The load of

    When we monitor, we monitor cpu The load of , So what is a load ? There is the concept of multi-core and multi thread in programming , that cpu How it works inside ? Find out how much bit cpu? There are several physics cpu? Every cpu It's several cores ? When I bought a memory module before , It needs to be closed ...

  9. CPU | Physics CPU vs Logic CPU vs The core vs Threads vs Socket

    When we try to pass Linux command nproc and lscpu Learn about a computer CPU Level architecture and performance , We always find that we can't understand the results correctly , Because we get confused by several terms : Physics CPU. Logic CPU. ...

Random recommendation

  1. Binary tree creation and traversal (C Version and java edition )

    Take this tree for example :# Indicates the preorder traversal of an empty node ( root -> Left -> Right ) by :ABD##E##C#F## In the sequence traversal ( Left -> root -> Right ) by :#D#B#E#A#C#F# After the sequence traversal ( Left -> Right -> ...

  2. GUI Mistakes in learning Exception in thread &quot;main&quot; java.lang.NullPointerException

    An error occurred while running :Exception in thread "main" java.lang.NullPointerException This problem is mostly due to the fact that an object is only declared , But not ...

  3. R Language Data Frame Common operations of data frame

    Data Frame It's usually translated as a data frame , It feels like R In the table , It consists of rows and columns , And Matrix The difference is , Each column can be a different data type , and Matrix It has to be the same . Data Frame Each column has a column name , Each line can also be ...

  4. ( turn ) Yes Oracle Export file error and DMP Analysis of file structure ,EXP-00008: encounter ORACLE error 904 ORA-00904: &quot;MAXSIZE&quot;: invalid identifier

    EXP-00008: encounter ORACLE error 904 ORA-00904: "MAXSIZE": invalid identifier reason :oracle The versions are different perform C:/ ...

  5. C# Right click Copy path

    using System;//Environment using System.Windows.Forms; //add referece of System.Windows.Forms :DataF ...

  6. rabbitmq Message persistence receive and send

    Two : Task distribution & Message persistence   When multiple receivers are enabled, if one of them receive Close to make sure the message has feedback   send End #-*- coding: UTF-8 -*-import pika ...

  7. Kcptun It's a very simple and fast , be based on KCP Agreed UDP Tunnel , It can be TCP Stream to KCP+UDP flow

    This blog has been published through Finalspeed Speed up Shadowsocks A tutorial for , It is generally reflected that we can achieve a very good speed .Finalspeed Is good , Just a little bit more memory , Not suitable for users with small server memory : and ...

  8. turn :swing in paint And paintComponent The difference between (jcomponent)

    http://blog.csdn.net/q597756870/article/details/17854247 check API file , It's in class Jcomponent Under the paint The method has the following explanation :        ...

  9. Oracle Intercept JSON String content

    CREATE OR REPLACE FUNCTION PLATFROM.parsejsonstr(p_jsonstr varchar2,startkey varchar2,endkey varchar ...

  10. haproxy Introduction of monitoring page addition and parameters ( turn )

    Environmental Science : [root@localhost 13:55:31 haproxy]# cat /etc/redhat-release CentOS release 6.8 (Final) [root@loca ...