python process, thread, coroutine

process

A process is an executing program. Each process has its own address space, memory, data stack, and other auxiliary data used to track execution. In the multi-process in a single-core CPU system, there can be many programs in the memory, but only one program is running at a given moment; that is, it is possible that process A is running in one second, and process B is running in the next second, although Both are in memory and neither are really running at the same time.
The process is the smallest unit of system resource allocation; the process has its own independent memory space (data is not shared, high overhead)
1. Use Process to achieve multiple processes

from multiprocessing import Process, Pool

def do_work():
    print('进程正在执行, 进程ID: {}'.format(Process.pid))

if __name__ == '__main__':
    p = Process(target=do_work)
    p2 = Process(target=do_work)
    p.start()
    p2.start()
    p.join()
    p2.join()

Two. Use process pool to achieve multi-process

from multiprocessing import Process, Pool

def do_work():
    print('进程正在执行, 进程ID: {}'.format(Process.pid))

if __name__ == '__main__':
    pool = Pool(processes=2)
    for i in range(0, 2):
        pool.apply_async(do_work)
    pool.close()
    pool.join()

Three. The standard library ProcessPoolExecutor of the process pool after python3.2

from concurrent.futures import ProcessPoolExecutor
from time import  sleep


def loop(nloop, nsec):
    print("start process", nloop)
    sleep(nsec)
    print("end process", nloop)


if __name__=="__main__":
    with ProcessPoolExecutor(max_workers=3) as executor:
        all_task = [executor.submit(loop, i, j) for i, j in zip([1, 2], [4, 3])]

Thread

The thread is the smallest unit of scheduling execution, subordinate to the process, and the actual executor of the program. A process contains at least one main thread, and it can also have more child threads. Python can run multi-threaded, but like single-core CPU multi-process, only one thread will execute at a given time.
Multiple threads share memory (data sharing, shared global variables) to improve the efficiency of program operation
1. Use threading to create multiple threads

from threading import Thread
from time import sleep


def do_work(params):
    print('start Thread {}'.format(params))
    sleep(5)
    print('end Thread {}'.format(params))


if __name__ == '__main__':
    th = Thread(target=do_work, args=('th', ))
    th2 = Thread(target=do_work, args=('th2', ))
    th.start()
    th2.start()
    th.join()
    th2.join()

2. Use thread pool to create multiple threads

from concurrent.futures import ThreadPoolExecutor


def do_work(params):
    print('start Thread {}'.format(params))
    sleep(5)
    print('end Thread {}'.format(params))

if __name__ == '__main__':
    with ThreadPoolExecutor(max_workers=3) as executor:
        all_task = [executor.submit(do_work, i) for i in range(1, 3)]

Coroutine

Coroutine: a lightweight thread in user mode, the scheduling of the collaboration is completely controlled by the user; it is a more lightweight existence than threads. Just as a process can have multiple threads, a thread can also have multiple coroutines. Most importantly, coroutine not be managed by the operating system kernel, and is completely controlled by the program
coroutine has its own register context and stacks;
when coroutine scheduled handover, save the register context and stack to another location;
cutting When you come back, restore the previously saved register context and stack;
directly manipulate the stack without the overhead of kernel operations, access global variables without locks, and context switches quickly;
one.gevent implements a coroutine

import gevent


def eat(name):
    print('%s eat 1' %name)
    gevent.sleep(2)
    print('%s eat 2' %name)


def play(name):
    print('%s play 1' %name)
    gevent.sleep(1)
    print('%s play 2' %name)


if __name__ == '__main__':
    
    g1 = gevent.spawn(eat, 'egon')
    g2 = gevent.spawn(play, name='egon')
    g1.join()
    g2.join()

Guess you like

Origin blog.csdn.net/xxy_yang/article/details/107915872