The full-GIL global interpreter lock C language solves top PS

Source: Internet
Author: User

1.GIL Global Interpreter Lock: Ensures that only one thread is running at the same time. What is global interpreter lock Gil (Global interpreter Lock)
The execution of Python code is controlled by the Python virtual machine (also known as the main interpreter loop, CPython version), and Python is designed to take into account the main loop of the interpreter, while only one thread is executing, that is, at any moment, only one thread is running in the interpreter. Access to the Python virtual machine is controlled by the global Interpreter Lock (GIL), which ensures that only one thread is running at the same time.

1) Main thread dead loop
# main thread dead loop, full CPU  while True:     Pass

    

2) 2 thread dead loops

    

# -*-coding:utf-8-*- Import Threading # child thread Dead loop def Test ():      while True:         Pass  = Threading. Thread (target=test) T1.start ()# main thread dead loop  while True:     Pass

    

    • Multi-Threading in Python is actually fake, and efficiency is not as high as the process

3) 2 processes to achieve a dead loop
Import Multiprocessing def Deadloop ():      while True:         Pass # child process dead loop p1 = multiprocessing. Process (target=deadloop) p1.start ()# main process dead loop Deadloop ()

2.C language to solve the Gil problem under multithreading 1) test.c execution process
# include "Stdio.h" int main () {    printf ("Hello world\n");     return 0;}
    • GCC generates a.out files
[Email protected]:~/python06/06-gil/01- to solve GIL problem in C language $ gcc test.c [email protected]:~/python06/06-gil/01- solve Gil problem in C language $ lsa.out  test.c
    • ./a.out Execution procedure
[Email protected]:~/python06/06-gil/01-to solve GIL problem in C language./a.out Hello World

2) Solve the problem: The loop is written in C language.
# # # read.me  file  -shared-o libxxxx.so
    • 1.c language Write Loop loop
# # # loop.c  file void Deadloop ()    {while (1)    {        ;    }} 

    • 2. Execute command to generate the library file
GCC loop.c-shared-o libdeadloop.so.├──libdeadloop.so├──loop.c├──main.py└──read.me

    • 3. Loading the dynamic library
## # main.py file fromcTYPESImport* fromThreadingImportThread#loading the dynamic libraryLib = cdll. LoadLibrary ("./libdeadloop.so")#Create a child thread that executes the C language function, which is a dead loopt = Thread (target=Lib. Deadloop) T.start ()#The main thread, also called the C language written by the dead loop function#Lib. Deadloop () whileTrue:Pass

    • 4. Perform Python3 main.py to view the

    

3.htop top real-time process viewer
Htop is an interactive process viewer in a Linux system, and a text-mode application (in the console or X terminal) requires ncurses. Htop is more user-friendly than the traditional Linux top. It allows users to interact interactively, support color themes, scroll through the list of processes horizontally or vertically, and support mouse actions.

Compared to top, Htop has the following advantages:

    • You can scroll through the list of processes horizontally or vertically to see all the processes and the complete command line.
    • On startup, it's faster than top.
    • Process number is not required to kill the process.
    • Htop supports mouse operation.
    • Top is already very old.
      

4.ps One-time Process view command

      

The full-GIL global interpreter lock C language solves top PS

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.