Tag Archives: solution

Solution to the failure of springboot integrated PageHelper

     <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

 

The resolution of Ubuntu 16.04 screen can’t be modified and the solution of circulating login

Today, there are many problems with the graphics card driver. One of them is that after the computer restarts, the resolution of the display screen becomes 800600, and the normal resolution is 19201080. There is no other resolution option in the system settings, so it cannot be modified. Try to create the xorg. Conf file, which is described on the Internet, to set the custom desktop resolution. After the modification, the resolution has changed, but it is 1600 * 1200, which is still very abnormal. Using the combination of CVT and xrandr to modify the resolution indicates another error, xrandr: failed to get size of gamma for output default. One problem after another, what a tangle!

It has to be said that it’s really unpleasant to search these professional problems with Baidu in China. What often appears are some irrelevant or unsolvable web pages. Fortunately, it’s all solved now, and the problem is still in the driver side of the graphics card [1]. Software & amp; update in system settings; In updates, click additional drivers and change it to NVIDIA driver (I choose the third one here, as shown in the figure below). The application modification needs to wait for a little time, restart the computer after completion, and the resolution is normal again.

After the driver is re installed, the problem of circular login appears again. You may encounter it later. Record the solution here.

ctrl +alt +f2  
sudo service lightdm stop  
sudo apt-get --purge remove nvidia-*  

After many months, I stepped on the big pit of circular login again, but the previous solution was invalid. Another possible problem is that the owner and all groups of the. Xauthority file become root. There is a. Xauthority file in the user’s home directory. View the owner and all groups of the file

ls -la .Xauthority

If it is root, you need to change it to your login user:

sudo chown username:username .Xauthority

(there will always be all kinds of accidents in the actual solution, one by one, don’t worry.)

The range of Chinese Unicode encoding and the calculation of the number of Chinese and English words in text

Unicode encoding range of Chinese characters

Unicode encoding range
u4e00 ~ u9fff

U + 4e00 ~ U + 9fa5 is the most commonly used range, that is, the block named CJK unified ideographs. The characters between U + 9fa6 ~ U + 9fff are still empty codes, which have not been defined yet, but there is no guarantee that they will not be defined in the future

def is_zh(char):
    """

    :param char:  Single character
    :return:
    """
    
    if u'\u4e00' <= char <= u'\u9fff':
        return True
    return False

Statistics of Chinese and English words

In word document, Review – > count the number of words can calculate the number of words, Chinese words, non Chinese words and so on, now use Python to achieve
example: Hello, world 4

# -*- coding: utf-8 -*-
import re

def strQ2B(ustring):
	# Full to half angle string
    rstring = ""
    for uchar in ustring:
        inside_code = ord(uchar)
        if inside_code == 12288: # convert full-corner spaces directly
            inside_code = 32
        elif (inside_code >= 65281 and inside_code <= 65374): # Full-angle characters (except spaces) are converted according to the relationship
            inside_code -= 65248
        rstring += chr(inside_code)
    return rstring


def querySimpleProcess(ss):
	# query preprocessing, excluding characters other than Chinese and English numbers, all converted to lowercase
    s1=strQ2B(ss)
    s2=re.sub(r"(?![\u4e00-\u9fff]|[0-9a-zA-Z])."," ",s1) 
    s3=re.sub(r"\s+"," ",s2)
    return s3.strip().lower()


# Determine if it contains Chinese
def check_contain_chinese(check_str):
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False


# Determine if it contains English
def check_contain_english(check_str):
    for ch in check_str:
        if u'a' <= ch <= u'z' or u'A' <= ch <= u'Z':
            return True
    return False


# Delete letters from a string for character counting purposes
def delete_letters(ss):
    rs = re.sub(r"[a-zA-Z]+","",ss)
    return rs


# First space split, get the list, and then line processing each element in the list
###Example: Smart School Uniform Commercial=6, Disrespectful Breakup=2
### Exception: C Mile C Mile=3 ### Can't handle
# If the element does not contain Chinese, then the length of the element is recorded as: 1 + the number of digits
# If the element does not contain English, the length of the element is recorded as: the number of Chinese characters + the number of digits, you can directly use the len () method
# If the element contains both English and Chinese, the length of the element is recorded as: number of Chinese characters + number of digits + 1
def countCharacters(inputStr):
    tmpStr = querySimpleProcess(inputStr)
    str2list = tmpStr.strip().split(" ")
    if len(str2list) > 0:
        charsNum = 0  # Initialize character count
        for elem in str2list:
            chineseFlag = check_contain_chinese(elem)
            englishFlag = check_contain_english(elem)
            if englishFlag == False:  # no English
                charsNum = charsNum + len(elem)
                continue
            else:  # contain English
                elem = delete_letters(elem)
                charsNum = charsNum + 1 + len(elem)
        return charsNum
    return 0

[CHM] Python: How to Extract CHM Data

Demand scenario

Chm format document, extract and save as HTML

Method 1: use online conversion tools or software

Method 2: Script Compilation

Step 1: decompile. Bat script

Using this script, you can decompile the CHM file, decompress it and get the file. Some documents can be directly converted to TXT, and some will be converted to HTML. This has something to do with generating CHM files
1 create a new TXT document and write the command according to the example. After saving, save the file as . Bat format

hh -decompile Output path of conversion result File to be converted (relative/absolute path)
# Example
hh -decompile   D:\Desktop\  123.chm

2 right click the administrator to run the file, and output the result to the directory specified by the command

Step 2: HTML to TXT

[HTML] Python extracts HTML text to TXT

Pyyaml tutorial introduction to pyyaml library and YML writing and reading

PyYAML

Source code: https://github.com/yaml/pyyaml

install

# pip command line installation
pip install PyYAML

# Download the source code for installation
python setup.py install

Import

import yaml

Read yaml file

def read_yaml(yml_file, mode='r', encoding='utf-8'):
    """ Read and convert the contents of yaml to Python objects

    :param yml_file:
    :param mode:
    :param encoding:
    :return:
    """
    # safe_load_all() Open multiple documents
    with open(yml_file, mode=mode, encoding=encoding) as y_file:
        # .load is a non-recommended and unsafe encoding method
        # content = yaml.load(y_file.read(), yaml.FullLoader)
        # .safe_load safe encoding method
        # If you don't trust the input stream, you should use:
        return yaml.safe_load(y_file)

Write yaml file

def write_yaml(yaml_file, data, mode='w', encoding='utf-8', is_flush=True):
    """ Converting Python objects to yaml

    :param yaml_file:
    :param data:
    :param mode:
    :param encoding:
    :param is_flush:
    :return:
    """
    with open(yaml_file, mode=mode, encoding=encoding) as y_file:
        # yaml.dump(data, stream=y_file)
        # allow_unicode Solve the problem of writing messy code
        yaml.safe_dump(data, stream=y_file, allow_unicode=True)
        if is_flush:
            y_file.flush()

[zipfile] Python packages files as zip packages & decompresses them

Using Python to compress files, you can use the zipfile library, which provides a very rich API
zipfile itself is a context manager, and you can use with . The following is a simple demo.

pack

import os
import zipfile


def file2zip(zip_file_name: str, file_names: list):
    """ Compress and store files in multiple folders as zip
    
    :param zip_file_name: /root/Document/test.zip
    :param file_names: ['/root/user/doc/test.txt', ...]
    :return: 
    """
    # Read-write mode ZipFile requires mode 'r', 'w', 'x', or 'a'
    # Compression mode ZIP_STORED: stored; ZIP_DEFLATED: compressed storage
    with zipfile.ZipFile(zip_file_name, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
        for fn in file_names:
            parent_path, name = os.path.split(fn)
            
            # zipfile built-in provides for storing files compressed in .zip files, arcname is the name of the file stored in the zip file
            # The given archive name is arcname (by default it will be the same as filename, but without the drive letter and will remove the opening path separator)
            zf.write(fn, arcname=name)
            
            # Equivalent to the following two lines of code
            # Switch directories to write the file directly. Without switching directories, the entire path to the file is created in the zip file
            # os.chdir(parent_path)
            # zf.write(name)


if __name__ == "__main__":
	zip_name = '/root/Document/test.zip'
	files = ['/root/user/doc/test.txt', '/root/user/doc/test1.txt']
	file2zip(zip_name , files)

decompression

def zip2file(zip_file_name: str, extract_path: str, members=None, pwd=None):
    """ Folder specified by the zip file content extraction value

    :param zip_file_name: the file to be extracted .zip r'D:\Desktop\tst\tst.zip'
    :param extract_path: the directory where the extracted file is saved r'D:\Desktop\tst\test\test'
    :param members: specify the files to extract, default all
    :param pwd: the password to extract the file
    :return:
    """
    with zipfile.ZipFile(zip_file_name) as zf:
        zf.extractall(extract_path, members=members, pwd=pwd)

Python file compression

zipfile. Pyzipfile inherits from zipfile. Zipfile , and has a special writepy to package . Py . PyC

Common configuration methods of nginx

nginx.conf

server 
   max-content-length

site-avalables site-enabled

The file name of this sample configuration file is Default
edit file: /etc/nginx/sites available/default

# If there are multiple servers, configure them here, and modify the proxy_pass 
upstream flask {
        server 127.0.0.1:5000;
        server 127.0.0.1:5001;
}
server {

        listen 80 default_server;
        listen [::]:80 default_server;
		
		# Set the maximum upload data size allowed by nginx service for users
		# Adjust the upload file size limit setting parameter according to business requirements
		client_max_body_size 10m;
		
        # Cache file size setting 0 means no limit
        # Use the default configuration, exceeding the size will result in an error.net::ERR_INCOMPLETE_CHUNKED_ENCODING error
        proxy_max_temp_file_size 0;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location/{
                # Requests are forwarded to the gunicorn server
                proxy_pass http://127.0.0.1:5000;
                
                # Requests are forwarded to multiple gunicorn servers
                # proxy_pass http://flask;
                
                # Set the request header and pass the header information to the server side 
                proxy_set_header Host $host;
                
                # Set request header and pass original request ip to gunicorn server
                proxy_set_header X-Real-IP $remote_addr;
        }
}

Another way of writing

Modify the nginx configuration file so that nginx receives the request and forwards it to the uwsgi server


upstream test_server{
	server 10.211.55.2:8000;
}

#gzip  on;
server {
	listen  8000;
	server_name api.baidu.site;

	location/{
		include uwsgi_params;
		uwsgi_pass test_server;
	}

}

server {
	listen       80;
	server_name  www.baidu.site;

	#charset koi8-r;

	#access_log  logs/host.access.log  main;
	location /xadmin {
		include uwsgi_params;
		uwsgi_pass test_server;
 	}

	location /ckeditor {
		include uwsgi_params;
		uwsgi_pass test_server;
	}

	location/{
		root   /home/python/Desktop/front_end_pc;
		index  index.html index.htm;
	}

	error_page   500 502 503 504  /50x.html;
	location = /50x.html {
		root   html;
	}

}

Restart and stop of nginx in Linux service

Case 1: nginx has service

Nginx installed in Linux will register service service by default. You can directly start, stop and restart according to the following operations

service nginx start  # start
service nginx stop  # stop
service nginx restart  # restart

Case 2: the original nginx service is not clear

Find the installation directory of nginx to understand the situation of nginx

# Find Nginx installation information
find/-name nginx

# Go to the nginx sbin directory and restart
. /nginx -s reload

# If you have modified the Nginx configuration, check the configuration with -t
. /nginx -t

# Check to see if it starts
ps aux | grep nginx
# or
ps -ef | grep nginx

Nginx more

Adding prefix to nginx agent of Python flash project
deployment of ngnix + gunicorn + Flash project in Ubuntu environment

[solution] pyinnstaller package EXE file tutorial

In business scenarios, Python is often required to develop some small programs/scripts/GUI interfaces, which can be delivered to Xiaobai or partners who do not have Python installed
Deng Deng Deng Deng
pyinstaler

Scene description

Use pyinnstaller to package Python scripts or projects to generate executable. Exe files. Pyinstaller is a very good free packaging tool, supports windows, Linux and MacOS, and perfectly supports 32-bit and 64 bit systems. In addition to pyinstaller, you can also use py2exe for packaging

Pyinstaller installation

pip install pyinstaller

Packing parameters

# Common packing parameters
# -F -D mutually exclusive parameters
-F package into an exe file, small projects can be used, slow to open, multiple .py package can not be used
-D default parameter, package the result into the created folder, you can see a lot of dependency files inside

# -w -c mutually exclusive parameters
-w use the project GUI interface, no cmd console
-c default parameter, use cmd console If the execution of the package file reports an error, you can try to use -c to show the console

-n the name of the execution project, default .py file name
-i package ico icon into exe file

--hidden-import package information when packaging
# It is highly recommended to package PyQt projects with the following parameters
--hidden-import PyQt5.sip

# Example package command
# Execute the package command in the root directory of the project
pyinstaller -w xxx.py --hidden-import PyQt5.sip
 
# The output of the package is in the dist folder in the project's root directory
# It is not recommended to use -F to package into an exe file as the saying goes: package for a while, open for 5 seconds
# Non-F command, static resources, built Sqlite database can be put directly into the dist generated folder

explain

Pyinstaller just packages Python interpreter and script into an executable file, which is different from compiling into real machine code. So the efficiency of the code is reduced, only to solve the operation difficulty of Xiaobai users. When packaging, we will analyze the project dependencies, package the related dependencies, and encrypt them. Different operating systems need to be packaged in different operating systems

 

Summary of solutions to packaging problems

Console debug
runs successfully locally. It is recommended to use the - C command when the EXE is packaged for the first time, so that possible error messages can be displayed in the console
if the - C is not used during packaging, the console will not appear or the console will flash by and the error message will not be seen, you can execute the EXE file fatal error failed to execute script file in the CMD command line window
pyinstaller. Some libraries are not packaged during packaging, The library in error reporting information can be packaged by -- hidden import error reporting package

Solutions to various errors in pyinstall packaging python3.6 + pyqt5

The result file is too large

Parameters in help

>>> pyinstaller -h
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]
                   [--add-data <SRC;DEST or SRC:DEST>]
                   [--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]
                   [--hidden-import MODULENAME]
                   [--additional-hooks-dir HOOKSPATH]
                   [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
                   [--key KEY] [-d {all,imports,bootloader,noarchive}] [-s]
                   [--noupx] [--upx-exclude FILE] [-c] [-w]
                   [-i <FILE.ico or FILE.exe,ID or FILE.icns>]
                   [--version-file FILE] [-m <FILE or XML>] [-r RESOURCE]
                   [--uac-admin] [--uac-uiaccess] [--win-private-assemblies]
                   [--win-no-prefer-redirects]
                   [--osx-bundle-identifier BUNDLE_IDENTIFIER]
                   [--runtime-tmpdir PATH] [--bootloader-ignore-signals]
                   [--distpath DIR] [--workpath WORKPATH] [-y]
                   [--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL]
                   scriptname [scriptname ...]

positional arguments:
  scriptname            name of scriptfiles to be processed or exactly one
                        .spec-file. If a .spec-file is specified, most options
                        are unnecessary and are ignored.

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         Show program version info and exit.
  --distpath DIR        Where to put the bundled app (default: .\dist)
  --workpath WORKPATH   Where to put all the temporary work files, .log, .pyz
                        and etc. (default: .\build)
  -y, --noconfirm       Replace output directory (default:
                        SPECPATH\dist\SPECNAME) without asking for
                        confirmation
  --upx-dir UPX_DIR     Path to UPX utility (default: search the execution
                        path)
  -a, --ascii           Do not include unicode encoding support (default:
                        included if available)
  --clean               Clean PyInstaller cache and remove temporary files
                        before building.
  --log-level LEVEL     Amount of detail in build-time console messages. LEVEL
                        may be one of TRACE, DEBUG, INFO, WARN, ERROR,
                        CRITICAL (default: INFO).

What to generate:
  -D, --onedir          Create a one-folder bundle containing an executable
                        (default)
  -F, --onefile         Create a one-file bundled executable.
  --specpath DIR        Folder to store the generated spec file (default:
                        current directory)
  -n NAME, --name NAME  Name to assign to the bundled app and spec file
                        (default: first script's basename)

What to bundle, where to search:
  --add-data <SRC;DEST or SRC:DEST>
                        Additional non-binary files or folders to be added to
                        the executable. The path separator is platform
                        specific, ``os.pathsep`` (which is ``;`` on Windows
                        and ``:`` on most unix systems) is used. This option
                        can be used multiple times.
  --add-binary <SRC;DEST or SRC:DEST>
                        Additional binary files to be added to the executable.
                        See the ``--add-data`` option for more details. This
                        option can be used multiple times.
  -p DIR, --paths DIR   A path to search for imports (like using PYTHONPATH).
                        Multiple paths are allowed, separated by ';', or use
                        this option multiple times
  --hidden-import MODULENAME, --hiddenimport MODULENAME
                        Name an import not visible in the code of the
                        script(s). This option can be used multiple times.
  --additional-hooks-dir HOOKSPATH
                        An additional path to search for hooks. This option
                        can be used multiple times.
  --runtime-hook RUNTIME_HOOKS
                        Path to a custom runtime hook file. A runtime hook is
                        code that is bundled with the executable and is
                        executed before any other code or module to set up
                        special features of the runtime environment. This
                        option can be used multiple times.
  --exclude-module EXCLUDES
                        Optional module or package (the Python name, not the
                        path name) that will be ignored (as though it was not
                        found). This option can be used multiple times.
  --key KEY             The key used to encrypt Python bytecode.

How to generate:
  -d {all,imports,bootloader,noarchive}, --debug {all,imports,bootloader,noarchive}
                        Provide assistance with debugging a frozen
                        application. This argument may be provided multiple
                        times to select several of the following options.

                        - all: All three of the following options.

                        - imports: specify the -v option to the underlying
                          Python interpreter, causing it to print a message
                          each time a module is initialized, showing the
                          place (filename or built-in module) from which it
                          is loaded. See
                          https://docs.python.org/3/using/cmdline.html#id4.

                        - bootloader: tell the bootloader to issue progress
                          messages while initializing and starting the
                          bundled app. Used to diagnose problems with
                          missing imports.

                        - noarchive: instead of storing all frozen Python
                          source files as an archive inside the resulting
                          executable, store them as files in the resulting
                          output directory.

  -s, --strip           Apply a symbol-table strip to the executable and
                        shared libs (not recommended for Windows)
  --noupx               Do not use UPX even if it is available (works
                        differently between Windows and *nix)
  --upx-exclude FILE    Prevent a binary from being compressed when using upx.
                        This is typically used if upx corrupts certain
                        binaries during compression. FILE is the filename of
                        the binary without path. This option can be used
                        multiple times.

Windows and Mac OS X specific options:
  -c, --console, --nowindowed
                        Open a console window for standard i/o (default). On
                        Windows this option will have no effect if the first
                        script is a '.pyw' file.
  -w, --windowed, --noconsole
                        Windows and Mac OS X: do not provide a console window
                        for standard i/o. On Mac OS X this also triggers
                        building an OS X .app bundle. On Windows this option
                        will be set if the first script is a '.pyw' file. This
                        option is ignored in *NIX systems.
  -i <FILE.ico or FILE.exe,ID or FILE.icns>, --icon <FILE.ico or FILE.exe,ID or FILE.icns>
                        FILE.ico: apply that icon to a Windows executable.
                        FILE.exe,ID, extract the icon with ID from an exe.
                        FILE.icns: apply the icon to the .app bundle on Mac OS
                        X

Windows specific options:
  --version-file FILE   add a version resource from FILE to the exe
  -m <FILE or XML>, --manifest <FILE or XML>
                        add manifest FILE or XML to the exe
  -r RESOURCE, --resource RESOURCE
                        Add or update a resource to a Windows executable. The
                        RESOURCE is one to four items,
                        FILE[,TYPE[,NAME[,LANGUAGE]]]. FILE can be a data file
                        or an exe/dll. For data files, at least TYPE and NAME
                        must be specified. LANGUAGE defaults to 0 or may be
                        specified as wildcard * to update all resources of the
                        given TYPE and NAME. For exe/dll files, all resources
                        from FILE will be added/updated to the final
                        executable if TYPE, NAME and LANGUAGE are omitted or
                        specified as wildcard *.This option can be used
                        multiple times.
  --uac-admin           Using this option creates a Manifest which will
                        request elevation upon application restart.
  --uac-uiaccess        Using this option allows an elevated application to
                        work with Remote Desktop.

Windows Side-by-side Assembly searching options (advanced):
  --win-private-assemblies
                        Any Shared Assemblies bundled into the application
                        will be changed into Private Assemblies. This means
                        the exact versions of these assemblies will always be
                        used, and any newer versions installed on user
                        machines at the system level will be ignored.
  --win-no-prefer-redirects
                        While searching for Shared or Private Assemblies to
                        bundle into the application, PyInstaller will prefer
                        not to follow policies that redirect to newer
                        versions, and will try to bundle the exact versions of
                        the assembly.

Mac OS X specific options:
  --osx-bundle-identifier BUNDLE_IDENTIFIER
                        Mac OS X .app bundle identifier is used as the default
                        unique program name for code signing purposes. The
                        usual form is a hierarchical name in reverse DNS
                        notation. For example:
                        com.mycompany.department.appname (default: first
                        script's basename)

Rarely used special options:
  --runtime-tmpdir PATH
                        Where to extract libraries and support files in
                        `onefile`-mode. If this option is given, the
                        bootloader will ignore any temp-folder location
                        defined by the run-time OS. The ``_MEIxxxxxx``-folder
                        will be created here. Please use this option only if
                        you know what you are doing.
  --bootloader-ignore-signals
                        Tell the bootloader to ignore signals rather than
                        forwarding them to the child process. Useful in
                        situations where e.g. a supervisor process signals
                        both the bootloader and child (e.g. via a process
                        group) to avoid signalling the child twice.

[example multitasking] Python multithreading module

Python has built-in threading module, which encapsulates the lower level thread module
for built-in methods, see the official document: threading – thread based parallelism

Multithreading execution

The main thread will wait for all the child threads to finish

#coding=utf-8
import threading
import time

def thread_test():
    print("test.")
    time.sleep(1)

if __name__ == "__main__":
    for i in range(5):
        t = threading.Thread(target=thread_test)
        t.start() 

View the number of threads

#coding=utf-8
import threading
from time import sleep, ctime

def a():
    for i in range(3):
        print("a...%d"%i)
        sleep(1)

def bbbbb():
    for i in range(3):
        print("bbbbb...%d"%i)
        sleep(1)

if __name__ == '__main__':
    print('---start---:%s'%ctime())
    t1 = threading.Thread(target=a)
    t2 = threading.Thread(target=bbbbb)

    t1.start()
    t2.start()

    while True:
        length = len(threading.enumerate())
        # length = threading.active_count()
        print('The number of threads currently running is: %d'%length)
        if length<=1:
            break

        sleep(0.5)

Encapsulating the threading. Thread class

Inherit threading. Thread and override run method

#coding=utf-8
import threading
import time

class MyThread(threading.Thread):

    def run(self):
        for i in range(3):
            time.sleep(1)
            msg = "I'm "+self.name+' @ '+str(i) The name of the current thread is stored in the #name attribute
            print(msg)


if __name__ == '__main__':
    t = MyThread()
    t.start()

Thread execution order

Each thread runs a complete run function, but the start order of the thread and the execution order of each loop in the run function cannot be determined.

Sharing global variables and passing parameters

# Shared global variables
from threading import Thread
import time

g_num = 100

def work1():
    global g_num
    for i in range(3):
        g_num += 1
    print("----in work1, g_num is %d---"%g_num)


def work2():
    global g_num
    print("----in work2, g_num is %d---"%g_num)


print("---Before the thread is created g_num is %d---"%g_num)

t1 = Thread(target=work1)
t1.start()

#Delay for a while to make sure things are done in thread t1
time.sleep(1)

t2 = Thread(target=work2)
t2.start()

All threads in a process share global variables, which is very convenient to share data among multiple threads. Disadvantages: threads change global variables randomly, which may cause confusion among multiple threads (that is, threads are not safe)

from threading import Thread
import time

def work1(nums):
    nums.append(44)
    print("----in work1---",nums)


def work2(nums):
    #Delay for a while to make sure things are done in thread t1
    time.sleep(1)
    print("----in work2---",nums)

g_nums = [11,22,33]

t1 = Thread(target=work1, args=(g_nums,))
t1.start()

t2 = Thread(target=work2, args=(g_nums,))
t2.start()

Mutex

When multiple threads modify a shared data almost at the same time, synchronization control is needed.
when a thread wants to change the shared data, lock it first. At this time, the resource status is “locked”, and other threads cannot change it; Until the thread releases the resource and changes its state to “non locked”, other threads can lock the resource again. Mutex ensures that only one thread can write each time, so as to ensure the correctness of data in the case of multithreading.

Advantages ensure that a piece of key code can only be completely executed by one thread from beginning to end. Disadvantages 1 prevent the concurrent execution of multiple threads. In fact, a piece of code containing locks can only be executed in single thread mode, which greatly reduces the efficiency. Disadvantages 2 because there can be multiple locks, different threads hold different locks, and try to obtain the lock held by the other party, Deadlock may occur

import threading
import time

g_num = 0

def test1(num):
    global g_num
    for i in range(num):
        mutex.acquire()  
        g_num += 1
        mutex.release()  

    print("---test1---g_num=%d"%g_num)

def test2(num):
    global g_num
    for i in range(num):
        mutex.acquire()  
        g_num += 1
        mutex.release()  

    print("---test2---g_num=%d"%g_num)

# Create a mutually exclusive lock
# Default is the unlocked state
mutex = threading.Lock()

# Create 2 threads and have them each add 1000000 times to g_num
p1 = threading.Thread(target=test1, args=(1000000,))
p1.start()

p2 = threading.Thread(target=test2, args=(1000000,))
p2.start()

# Wait for the calculation to complete
while len(threading.enumerate()) ! = 1:
    time.sleep(1)

print("The final result after 2 threads operate on the same global variable is:%s" % g_num)

Mutex can use context manager
to use lock, condition and semaphore in with statement
the object with acquire() and release() method provided by this module can be used as context manager of with statement. When entering a statement block, the acquire () method will be called, and when exiting a statement block, release () will be called. Therefore, the following fragments:

with some_lock:
    # do something...

# Equivalent to :

some_lock.acquire()
try:
    # do something...
finally:
    some_lock.release()

Lock, RLOCK, condition, semaphore, and boundedsemaphore objects can now be used as context managers for the with statement.

Multithreading case [chat]

Write a program with two threads
thread 1 is used to receive data and then display
thread 2 is used to detect keyboard data and then send data through UDP

import socket
import threading


def send_msg(udp_socket):
    """Get the keyboard data and send it to the other side """
    while True:
        # 1. input data from keyboard
        msg = input("\nPlease enter the data to be sent:")
        # 2. enter the ip address of the other party
        dest_ip = input("\nPlease enter the other party's ip address:")
        # 3. enter the other party's port
        dest_port = int(input("\nPlease enter the other party's port:"))
        # 4. send data
        udp_socket.sendto(msg.encode("utf-8"), (dest_ip, dest_port))


def recv_msg(udp_socket):
    """Receive data and display """
    while True:
        # 1. receive data
        recv_msg = udp_socket.recvfrom(1024)
        # 2. decode
        recv_ip = recv_msg[1]
        recv_msg = recv_msg[0].decode("utf-8")
        # 3. display the received data
        print(">>>%s:%s" % (str(recv_ip), recv_msg))


def main():
    # 1. Create a socket
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # 2. bind local information
    udp_socket.bind(("", 7890))

    # 3. create a child thread to receive data
    t = threading.Thread(target=recv_msg, args=(udp_socket,))
    t.start()
    # 4. let the main thread detect the keyboard data and send
    send_msg(udp_socket)


if __name__ == "__main__":
    main()

[problem solving] the problem of err returned non zero status 1 or status 2 is solved after upgrading python3 in Ubuntu

Problem scenario
ubuntn originally has Python 3.5. Now, after upgrading and installing Python 3.7, the following problems appear in multiple scenarios:
problem 1 status 1

subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.

Question 2 status 2

returned non-zero exit status 2.

resolvent

# Find the lsb_release file first, you may need to add sudo
find/-name lsb_release
# Delete when found
rm -rf /usr/bin/lsb_release

Attachment:
I refer to the process notes of upgrading python3.7.1 under Ubuntu (recommended)