沙雕漫画——进程和线程(一)

作为小白的我,总是被多进程和多线程弄晕,当然,很多面试官也很喜欢问此类问题,针对这个问题,特意制作这个沙雕漫画来帮助小白的理解,同时加深对进程和线程的印象。
在这里插入图片描述
在这里插入图片描述

看完了以上的漫画,想必大家对进程线程有了一定的了解,那么接下来,我就举一个生活中常见的例子,来帮助小白的理解。
1.单核cpu
在这里插入图片描述
假设只有一个线程,来看看要用多久。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : xueli
# @Software: win10 Tensorflow1.13.1 python3.6.3
import time
import datetime


def func1(work):
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(4)

def func2(work):
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(3)

def func3(work):
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(4)

def func4(work):
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(1)

if __name__ == '__main__':
    start = time.time()
    func1(u'煮饭')
    func2(u'炒菜')
    func3(u'洗衣服')
    func4(u'拖地')
    print("总时:" ,time.time() - start)

运行结果:
在这里插入图片描述
接下来引入多线程:

import threading
import time
import datetime
import os

def func1(work):
    print('当前进程: {}'.format(os.getpid()))
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(4)
    print('%s,我已经完成了——>%s' % (datetime.datetime.now(), work))

def func2(work):
    print('当前进程: {}'.format(os.getpid()))
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(3)
    print('%s,我已经完成了——>%s' % (datetime.datetime.now(), work))

def func3(work):
    print('当前进程: {}'.format(os.getpid()))
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(4)
    print('%s,我已经完成了——>%s' % (datetime.datetime.now(), work))

def func4(work):
    print('当前进程: {}'.format(os.getpid()))
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(1)
    print('%s,我已经完成了——>%s' % (datetime.datetime.now(), work))


if __name__ == '__main__':
    print('当前主进程: {}'.format(os.getpid()))
    start = time.time()
    threads = []
    t1 = threading.Thread(target=func1, args=(u'煮饭',))
    threads.append(t1)
    t2 = threading.Thread(target=func2, args=(u'炒菜',))
    threads.append(t2)
    t3 = threading.Thread(target=func3, args=(u'洗衣服',))
    threads.append(t3)
    t4 = threading.Thread(target=func4, args=(u'拖地',))
    threads.append(t4)
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    print("共计用时{}秒".format((time.time()-start)))

运行结果:
在这里插入图片描述
这里只有一个进程。
2.多核CPU(以4核为例)
在这里插入图片描述

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/4/24 14:15
# @Author  : xueli
# @File    : process.py
# @Software: win10 Tensorflow1.13.1 python3.6.3

import time
import datetime
import os
from multiprocessing import Process

def func1(work):
    print('当前进程: {}'.format(os.getpid()))
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(4)
    print('%s,我已经完成了——>%s' % (datetime.datetime.now(), work))

def func2(work):
    print('当前进程: {}'.format(os.getpid()))
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(3)
    print('%s,我已经完成了——>%s' % (datetime.datetime.now(), work))

def func3(work):
    print('当前进程: {}'.format(os.getpid()))
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(4)
    print('%s,我已经完成了——>%s' % (datetime.datetime.now(), work))

def func4(work):
    print('当前进程: {}'.format(os.getpid()))
    print('%s,我现在要——>%s' %(datetime.datetime.now(),work))
    time.sleep(1)
    print('%s,我已经完成了——>%s' % (datetime.datetime.now(), work))


if __name__ == '__main__':
    print('当前主进程: {}'.format(os.getpid()))
    start = time.time()
    process = []
    p1 = Process(target=func1, args=(u'煮饭',))
    process.append(p1)
    p2 = Process(target=func2, args=(u'炒菜',))
    process.append(p2)
    p3 = Process(target=func3, args=(u'洗衣服',))
    process.append(p3)
    p4 = Process(target=func4, args=(u'拖地',))
    process.append(p4)
    for p in process:
        p.start()
    for p in process:
        p.join()
    print("共计用时{}秒".format((time.time()-start)))

运行结果:
在这里插入图片描述
这里创建了4个进程。由此可知,在这项工作中,单线程16秒,多线程4秒,多进程5.7秒。注意:进程与进程的切换要耗资源,所以平时工作中进程数不能开太大,进程数取决于CPU的核数。如有问题,欢迎大佬给予批评指点。

发布了8 篇原创文章 · 获赞 7 · 访问量 423

猜你喜欢

转载自blog.csdn.net/zcs_xueli/article/details/105728342