Multiprocess and multithreading

1. Multi process

Use the process class or pool thread pool

2. Multithreading

A process will automatically start a main thread, and the main thread can start other sub threads

Use the threading module to open the Thread class

import time, threading

# Code executed by the new thread:
def loop():
    print('thread %s is running...' % threading.current_thread().name)
    n = 0
    while n < 5:
        n = n + 1
        print('thread %s >>> %s' % (threading.current_thread().name, n))
        time.sleep(1)
    print('thread %s ended.' % threading.current_thread().name)

print('thread %s is running...' % threading.current_thread().name)
t = threading.Thread(target=loop, name='LoopThread')
t.start()
t.join()
print('thread %s ended.' % threading.current_thread().name)

The biggest difference between multithreading and multiprocessing is that in multiprocessing, a copy of the same variable exists in each process, and each copy does not affect each other; In multithreading, each thread shares the same variable

import time, threading

# Suppose this is your bank deposit:
balance = 0

def change_it(n):
    # Save before retrieve, the result should be 0:
    global balance
    balance = balance + n
    balance = balance - n

def run_thread(n):
    for i in range(2000000):
        change_it(n)

t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)

It is likely that the balance is changed in the process of changing the global variable balance alternately in t1t2. We must ensure that when one thread modifies the balance, another thread cannot change it.

What should I do? Lock! threading.lock() is a lock (mutex) threading Rlock() is also OK

balance = 0
lock = threading.Lock()

def run_thread(n):
    for i in range(100000):
        # To acquire a lock:
        lock.acquire()
        try:
            # Change it safely:
            change_it(n)
        finally:
            # Release the lock after modification:
            lock.release()

However, there is a problem that Python can not make full use of multi-core. This is because the interpreter has a GIL lock when executing code. Any thread will obtain the GIL lock before execution. After execution for a period of time, the GIL lock will be released to give other threads a chance to execute. Therefore, python cannot use multithreading to perform multi-core tasks, but can use multi-process to perform multi-core tasks. Gils between multiple processes are not affected

If several threads are allowed to be allowed together (imagine that in the past, a toilet was used by one person, but now a toilet has three positions and three people are used together)

import threading
import time

def run(n):
    semaphore.acquire()
    time.sleep(1)
    print(n)
    semaphore.release()

num=0
semaphore=threading.threading.BoundedSemaphore(3)
for i in range(22):
    t = threading.Thread(target=run, args=("t-%s" % i,))
    t.start()
while threading.active_count() != 1:
    pass  # print threading.active_count()
else:
    print('-----all threads done-----')

3. Regular expression

Regular expressions use re Match to match

\d stands for numbers, \ w stands for letters or numbers Represents anything, \ s represents a space

*Represents any length, + represents at least one character,? Represents zero or one, {n} represents n characters, {n,m} represents n to M characters

A|B represents a or B, ^ represents the beginning of the line, and $represents the end of the line

\d{3}\s+\d{3,8} represents three numbers + spaces of any length + 3-8 numbers

\d{3}\-\d{3,8} represents three numbers + - + 3-8 numbers

[] table range, [0-9a-zA-Z \] Indicates a numeric letter underscore

  • [0-9a-zA-Z\_]+ You can match a string consisting of at least one number, letter, or underscore, such as' a100 ',' 0_Z ',' Py3000 ', etc;

  • [a-zA-Z\_][0-9a-zA-Z\_]* You can match a string starting with a letter or underscore followed by any number, letter or underscore, that is, Python's legal variable;

  • [a-zA-Z\_][0-9a-zA-Z\_]{0, 19} more precisely limits the length of the variable to 1-20 characters (1 character before + 19 characters after).

You can use regular expressions to split characters

Like the traditional 'a b C' spilt(' ')

['a', 'b', '', '', 'c']

re.spilt(r[\s]+,'ab  c')

re.spilt(r[\s\,]+,'a,b c  d')

You can also extract strings

import re

match=re.match(r'(\d{3})\-(\d{3,8})','010-21313')
print(match.group(1))

The default is greedy matching

re.match(r'^(\d+)(0*)$','102300').groups()

If it's not a greedy match, add one? that will do

re.match(r'^(\d+?)(0*)$','102300').groups()

precompile

Precompiling can improve speed

import re
re_telephone=re.compile(r'^(\d{3})\-(\d{3,8})$')
re_telephone.match('010-12345').groups()

Added by martinstan on Mon, 03 Jan 2022 23:36:00 +0200