Practical article a

Python concurrent, parallel, synchronous, and asynchronous — Study notes

Concept of analytical

  • Serial: Do one thing at a time

  • Parallel: You can do more than one thing at a time

  • Concurrency v.S

    • Concurrency is when several programs are running on the same CPU at any one time, but only one program is running on the CPU at any one time (single-core switching).
    • Parallelism means that at any point in time, there are multiple programs running on multiple cpus at the same time, that is, each CPU runs a program independently (multi-core running at the same time).
    • The maximum number of parallelism is the same as the number of cpus.
  • Synchronous V.S. asynchronous

    • Synchronization refers to the way in which code calls an IO operation and waits for the return of the IO operation to complete
    • Asynchrony refers to the way that code invokes AN IO operation without waiting for the IO operation to complete
  • multithreading: alternate execution, another sense of serialSmaller than the concept of process, reduce the consumption of process switching process

  • Multiple processes: Parallel execution, true parallelismMulti-task parallelism is realized by switching different processes

  • Multiprocess V.S. multithreading

    • Python multithreading cannot take advantage of multiple cores due to the global explain lock GIL, so it is not suitable for computationally intensive tasks, and is suitable for IO intensive tasks.
    • Cpu-intensive tasks are suitable for multiple processes
    • Process switching costs more than threads
    • Threads can communicate with each other via global variables, but processes cannot. Data between processes is completely isolated.
  • When the class:

    Threading.lock () : Synchronization locks (mutex locks) to address data security issues

    Threading.rlock () : A recursive lock to solve thread deadlocks

    Threading.semaphore () : A Semaphore that allows up to n threads to run at the same time. Threads that exceed the Semaphore will be blocked until the Semaphore is released.

    Threading.event () : events that allow different threads to interact with each other in synchronization rather than running independently: you send me a signal, I receive an action, send you a signal, you receive an action and continue to signal…

The lock

import threading
Create lock object, globally unique
lock = threading.Lock()
# acquiring a lock
lock.acquire()
# releases the lock
lock.release()
Copy the code
  • Context management protocols can be used to manage locks
lock = threading.Lock()
with lock:
    pass
# is equivalent to:
lock.acquire()
try:
    pass
finally:
    lock.release()
Copy the code
  • Locking affects performance, and it takes time to acquire and release locks.
  • Using locks can cause deadlocks

multithreading

import threading
import time

def sleeper(n,name) :
    print('Hi, I`m {}. I`m going to sleep 5 seconds'.format(name))
    time.sleep(n)
    print('{} has woken up from sleep'.format(name))
    
e.g1.
def main() :
    t = threading.Thread(target = sleeper, args = (5.'thread1'))
    t.start()

    Call the.join () method to block the current thread until it finishes, and then jump to the rest of the threads
    # t.jin () If this statement is added, the program will wait for sleeper to complete before jumping back to print the next two times

    print('hello')
    print('hello')
>>
Hi, I`m thread1. I`m going to sleep 5 seconds.
hello
hello
thread1 has woken up from sleep.
Copy the code

Correct usage of start and join

# This is the correct way to use it
# start and join loops separately
import threading
import time

def sleeper(n,name) :
    print('Hi, I`m {}. I`m going to sleep 5 seconds'.format(name))
    time.sleep(n)
    print('{} has woken up from sleep'.format(name))

e.g2.
def main() :
    start = time.time()
    thread_list = []
    for i in range(1.6):
        t = threading.thread(target=sleeper, args=(5.'thread{}'.format(i)))
        thread_list.append(t)
        t.start()
        print('{} has started.'.format(t.name)
    
    # Use of join
    for t in thread_list:
        t.join()
    end = time.time()
    print('total time: {}'.format(end - start))
    print('aaa')  
              
>>
Hi, I`m thread1. I`m going to sleep 5 seconds
Thread-1 has started.
Hi, I`m thread2. I`m going to sleep 5 seconds
Thread-2 has started.
Hi, I`m thread3. I`m going to sleep 5 seconds
Thread-3 has started.
Hi, I`m thread4. I`m going to sleep 5 seconds
Thread-4 has started.
thread2 has woken up from sleep
thread1 has woken up from sleep
thread4 has woken up from sleep
thread3 has woken up from sleep
total time: 5.001285791397095
aaa              
Copy the code

Incorrect use of start and join

The root cause of the error is that the start and join modes are combined. Thread2. Start (), thread2. Join (), thread2. The main program will have to wait for thread2 to finish running before starting thread3, so our program will logically become single-threaded again, so joining thread.start() directly after thread.start() is the wrong way
This is the wrong way to use it
import threading
import time

def sleeper(n,name) :
    print('Hi, I`m {}. I`m going to sleep 5 seconds'.format(name))
    time.sleep(n)
    print('{} has woken up from sleep'.format(name))

e.g3.
def main() :
    start = time.time()
    thread_list = []
    for i in range(1.6):
        t = threading.thread(target=sleeper, args=(5.'thread{}'.format(i)))
        thread_list.append(t)
        t.start()
        t.join() # block process
        print('{} has started.'.format(t.name)

    end = time.time()
    print('total time: {}'.format(end - start))
    print('aaa')  
              
>>
Hi, I`m thread1. I`m going to sleep 5 seconds
thread1 has woken up from sleep
Thread-1 has started.
Hi, I`m thread2. I`m going to sleep 5 seconds
thread2 has woken up from sleep
Thread-2 has started.
Hi, I`m thread3. I`m going to sleep 5 seconds
thread3 has woken up from sleep
Thread-3 has started.
Hi, I`m thread4. I`m going to sleep 5 seconds
thread4 has woken up from sleep
Thread-4 has started.
total time: 20.00114393234253
aaa              
Copy the code