前言

    记录一下多进程的学习

1.多线程?多进程?

    我们知道,python中的多线程其实是一个"假"的多线程,不管你CPU有多少核,python多线程在同一时间内只能在一个核上执行一条程序,python的多线程只不过是利用CPU上下文切换的优势,让我们看起来像是并发的效果.
    那么什么时候可以用pythond多线程呢?
    python的多线程不适合CPU密集型任务,而是适合I/O操作密集型的任务.
    如果有CPU密集型任务的话python该怎么办?这个时候就用到了多进程.进程之间是相互独立的,多进程可以使用CPU的多核.
    我们可以这样做:假如你的电脑是8核的,我们就可以启动8个进程,由于每个进程下最少有一个线程,这样的话就相当于有8个线程,这个时候就可以利用多核的优势了.这样做唯一的坏处就是多个线程之间的数据是不共享的.(由于每个线程都是在独立的进程下启动的.)

2.python多进程–multiprocessing

    python多进程的使用方法和多线程的使用方法类似:

import multiprocessing

import time

def func(name):
    time.sleep(2)
    print('hello,',name)

if __name__ == '__main__':
    start_time = time.time()
    processing_list = []
    for i in range(10):
        p = multiprocessing.Process(target=func,args=('Bob {}'.format(i),))
        p.start()
        processing_list.append(p)

    for p in processing_list:
        p.join()


    end_time = time.time()
    print('cost time {}s'.format((end_time-start_time)))

执行结果:

hello, Bob 0
hello, Bob 1
hello, Bob 2
hello, Bob 3
hello, Bob 4
hello, Bob 5
hello, Bob 6
hello, Bob 8
hello, Bob 7
hello, Bob 9
cost time 2.014963388442993s

3.多进程下启动多线程

import multiprocessing
import threading
import time


def thread_run(name):
    print('子线程',name)
    print(threading.get_ident())

def func(name):
    time.sleep(2)
    print('hello,',name)
    t = threading.Thread(target=thread_run,args=(name,))
    t.start()

if __name__ == '__main__':
    start_time = time.time()
    processing_list = []
    for i in range(10):
        p = multiprocessing.Process(target=func,args=('Bob {}'.format(i),))
        p.start()
        processing_list.append(p)

    for p in processing_list:
        p.join()


    end_time = time.time()
    print('cost time {}s'.format((end_time-start_time)))

执行结果:

hello, Bob 2
hello, Bob 0
hello, Bob 1
子线程 Bob 0
140707040950016
子线程 Bob 2
140707040950016
子线程 Bob 1
140707040950016
hello, Bob 4
hello, Bob 5
子线程 Bob 4
140707040950016
hello, Bob 3
hello, Bob 6
hello, Bob 7
子线程 Bob 3
140707040950016
子线程 Bob 6
140707040950016
子线程 Bob 7
140707040950016
hello, Bob 9
hello, Bob 8
子线程 Bob 9
140707040950016
子线程 Bob 8
140707040950016
cost time 2.0153234004974365s

程序解释:

  • 在进程中启动线程:
def func(name):
    time.sleep(2)
    print('hello,',name)
    t = threading.Thread(target=thread_run,args=(name,))
    t.start()

注:t = threading.Thread(target=thread_run,args=(name,))是启动一个线程,而func()函数则是一个进程,这样就相当于在进程中启动了一个线程.

4.查看进程id

from multiprocessing import Process
import os


def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())
    print("\n\n")


def f(name):
    info('\033[31;1mcalled from child process function f\033[0m')
    print('hello', name)

if __name__ == '__main__':
    info('\033[32;1mmain process line\033[0m')
    for i in range(3):
        p = Process(target=f, args=('bob {}'.format(i),))
        p.start()
    # p.join()

执行结果:

main process line
module name: __main__
parent process: 6265
process id: 24994



called from child process function f
module name: __main__
parent process: 24994
process id: 24995



hello bob 0
called from child process function f
module name: __main__
parent process: 24994
process id: 24996



hello bob 1
called from child process function f
module name: __main__
parent process: 24994
process id: 24997



hello bob 2

可以看到,主函数main是主进程,我们在程序里开启的进程都是它的子进程.

os.getppid()		# 获取父进程id
os.getpid()			# 获取当前进程id

5.将进程定义成类

import multiprocessing
import time
import os

def func(name):
    time.sleep(2)
    print('hello,',name)

class MyProcessing(multiprocessing.Process):
    def __init__(self,name):
        super(MyProcessing, self).__init__()
        self.name = name

    def run(self):
        print('父进程',os.getppid())
        print('当前进程',os.getpid())
        func(self.name)

if __name__ == '__main__':
    start_time = time.time()
    processing_list = []
    for i in range(3):
        p = MyProcessing('Bob {}'.format(i))
        p.start()
        processing_list.append(p)

    for p in processing_list:
        p.join()

    end_time = time.time()
    print('cost time {}s'.format((end_time - start_time)))

执行结果:

父进程 25858
当前进程 25859
父进程 25858
当前进程 25860
父进程 25858
当前进程 25861
hello, Bob 0
hello, Bob 1
hello, Bob 2
cost time 2.0101070404052734s

写在最后

    本文是个人的一些学习笔记,如有侵权,请及时联系我进行删除,谢谢大家.