Python 并发部分的面试题

进程

  • 进程间内存是否共享?如何实现通讯?

    进程间内存不共享,可以通过

    1. Manage模块加锁
    2. 通过队列或
    3. 通过管道加锁
    4. socket实现通讯
  • 请聊聊进程队列的特点和实现原理?
    1. 先进先出 Queue
    2. 后进先出 LifoQueue
    3. 优先级队列 PriorityQueue
    4. 线程本身带锁通过put()数据和get()数据,同一时间只有一个线程运行修改任务实现数据安全
  • 请画出进程的三状态转换图
    就绪====运行
       \阻塞/
  • 你了解生产者模型消费者模型么?如何实现?
yield:
def consumer():
    n = yield
    n = yield
    n = yield
    n = yield
    n = yield
    n = yield
    n = yield
    n = yield

def producer():
    g = consumer()
    next(g)
    for i in range(2000000):
        g.send(i)
  • 从你的角度说说进程在计算机中扮演什么角色?

    进程在计算机中扮演数据集的调用调配角色。

    负责回收和控制子线程的运行,是一个数据集。

    ##线程

  • GIL锁是怎么回事?

    GIL锁是全局解释器锁,只有CPython中使用,同一时间只能有一个线程调度CPU

  • 在python中是否线程安全?

    不安全,需要加锁才安全

  • 什么叫死锁?

    同时满足两个条件锁才能解开,分别有两把或以上的锁,有多个线程分别抢占了两个条件中的锁,互不释放造成阻塞,死锁现象。

  • logging模块是否是线程安全的?

    logging模块是线程安全的,因为使用的是单例设计模式。

  • threading.local的作用?

    Python提供了 threading.local 类,将这个类实例化得到一个全局对象,

    但是不同的线程使用这个对象存储的数据其它线程不可见(本质上就是不同的线程使用这个对象时为其创建一个独立的字典)。

  • 程序从flaga执行到falgb的时间大致是多少秒?

    60s 因为没有设置守护线程,需要等子线程跑完主线程才结束

import threading
import time

def _wait():
    time.sleep(60)
# flag a
t = threading.Thread(target=_wait, daemon=False)
t.start()
# flag b
  • 程序从flaga执行到falgb的时间大致是多少秒?

    0.01s 因为设置守护线程,子线程等待所有非子线程结束子线程结束。

import threading
import time

def _wait():
    time.sleep(60)
# flag a
t = threading.Thread(target=_wait, daemon=True)
t.start()
# flag b
  • 程序从flaga执行到falgb的时间大致是多少秒?

    60s 因为设置了阻塞需要等待子线程结束主线程才能结束

import threading
import time

def _wait():
    time.sleep(60)
# flag a
t = threading.Thread(target=_wait, daemon=True)
t.start()
t.join()
# flag b
  • 读程序,请确认执行到最后number是否一定为0

    不一定,因为存在线程安全问题,同一时间修改变量值

import threading
loop = int(1E7)

def _add(loop: int = 1):
    global number
    for _ in range(loop):
        number += 1

def _sub(loop: int = 1):
    global number
    for _ in range(loop):
        number -= 1
number = 0

ta = threading.Thread(target=_add, args=(loop,))
ts = threading.Thread(target=_sub, args=(loop,))
ta.start()
ts.start()
ta.join()
ts.join()
print(number)
  • 读程序,请确认执行到最后number是否一定为0

    一定为0 ,因为增删操作只有一条语句不会产生数据安全问题。

import threading
loop = int(1E7)
def _add(loop: int = 1):
    global number
    for _ in range(loop):
        number += 1

def _sub(loop: int = 1):
    global number
    for _ in range(loop):
        number -= 1
number = 0
ta = threading.Thread(target=_add, args=(loop,))
ts = threading.Thread(target=_sub, args=(loop,))
ta.start()
ta.join()
ts.start()
ts.join()
print(number)
  • 读程序,请确认执行到最后number的长度是否一定为1

    不一定为1,因为产生了阻塞可能有数据安全问题。

import threading
loop = int(1E7)

def _add(loop: int = 1):
    global numbers
    for _ in range(loop):
        numbers.append(0)

def _sub(loop: int = 1):
    global number
    while not numbers:
        time.sleep(1E-8)
    numbers.pop()
numbers = [0]
ta = threading.Thread(target=_add, args=(loop,))
ts = threading.Thread(target=_sub, args=(loop,))
ta.start()
ta.join()
ts.start()
ts.join()
print(numbers)
  • 读程序,请确认执行到最后number的长度是否一定为1

    不一定为1,因为产生了阻塞可能有数据安全问题。

import threading
loop = int(1E7)

def _add(loop: int = 1):
    global numbers
    for _ in range(loop):
        numbers.append(0)

def _sub(loop: int = 1):
    global number
    while not numbers:
        time.sleep(1E-8)
    numbers.pop()
numbers = [0]
ta = threading.Thread(target=_add, args=(loop,))
ts = threading.Thread(target=_sub, args=(loop,))
ta.start()
ts.start()
ta.join()
ts.join()
print(numbers)
  • 什么是协程?常用的协程模块有哪些?

    在线程遇到阻塞的情况时切换到另一个线程继续执行,如果遇到阻塞再到另一个线程执行。如果跳转过的线程转为就绪状态就直接执行,

    知道程序中没有阻塞。有gevent,和greenlet.

  • 协程中的join是用来做什么用的?它是如何发挥作用的?

    协程是遇到阻塞才切换,join负责阻塞开始执行程序。

  • 使用协程实现并发的tcp
server端
from gevent import monkey;monkey.patch_all()
import gevent
import time
import random
import socket

sk = socket.socket()
server_addr = ('127.0.0.1',9988)
sk.bind(server_addr)
sk.listen()

def listen(conn):
    while True:
        msg = conn.recv(1024).decode('utf-8')
        if msg == 'q':break
        conn.send(msg.upper().encode('utf-8'))

if __name__ == '__main__':
    while True:
        conn,addr = sk.accept()
        gevent.spawn(listen,conn)
  • 在一个列表中有多个url,请使用协程访问所有url,将对应的网页内容写入文件保存
from gevent import monkey;monkey.patch_all()
import time
import random
import gevent
from urllib.request import urlopen
url_dic = {
   '协程':'http://www.cnblogs.com/Eva-J/articles/8324673.html',
   '线程':'http://www.cnblogs.com/Eva-J/articles/8306047.html',
   '目录':'https://www.cnblogs.com/Eva-J/p/7277026.html',
   '百度':'http://www.baidu.com',
   'sogou':'http://www.sogou.com',
   '4399':'http://www.4399.com',
   '豆瓣':'http://www.douban.com',
   'sina':'http://www.sina.com.cn',
   '淘宝':'http://www.taobao.com',
   'JD':'http://www.JD.com'
}
def getHtml(url):
   ret = urlopen(url)
   html = ret.read()
   html_file = url.replace('.','_').lstrip('https://').replace('/','_')+'.html'
   with open(html_file,mode='wb') as f:
       f.write(html)
   print(url+'done')

if __name__ == '__main__':
    print('start')
    lst = list()
    for url in url_dic:
       ret = gevent.spawn(getHtml,url_dic[url])
       lst.append(ret)

    for url in lst:
       url.join()
    print(1)

综合

  • 进程和线程的区别

    进程是计算机资源分配的最小单位,线程是cpu调度的最小单位,线程较进程来说较轻量,启动更迅速。调用更方便

  • 进程池、线程池的优势和特点

    进程池一般在高计算的条件下使用,资源消耗更多。

    线程池可以运行在一个进程中,执行效率更高,可控cpu使用率。节约资源。

  • 线程和协程的异同?

    协程,即协作式程序,其思想是,一系列互相依赖的协程间依次使用CPU,每次只有一个协程工作,而其他协程处于休眠状态。

    协程实际上是在一个线程中,只不过每个协程对CUP进行分时,协程可以访问和使用unity的所有方法和component

    线程,多线程是阻塞式的,每个IO都必须开启一个新的线程,但是对于多CPU的系统应该使用thread,尤其是有大量数据运算的时刻,

    但是IO密集型就不适合;而且thread中不能操作unity的很多方法和component

  • 请简述一下互斥锁和递归锁的异同?

    两者在在单锁的情况下都可以保证数据的可靠性。

    二者唯一的区别是,同一个线程可以多次获取同一个递归锁,不会产生死锁。而如果一个线程多次获取同一个非递归锁,则会产生死锁。

  • 请列举一个python中数据安全的数据类型?

    Queue

  • Python中如何使用线程池和进程池
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExector
p = ProcessPoolExector(4)
ret = p.submit(func,aa)
print(ret.result())
  • 简述

    进程、线程、协程的区别

    进程是:一个CPU情况下,多个程序分别使用机器资源(CPU或硬盘等)的概念;

    线程是:一个进程情况下,多个执行流程(即线程)分别使用分配给该进程的机器资源的概念;

    • 协程是:一个线程情况下,多个执行流程(即协程)由线程控制,分别使用分配给该线程的机器资源的概念;

      以及应用场景?

    • 多进程:密集CPU任务,需要充分使用多核CPU资源(服务器,大量的并行计算)的时候,用多进程。 multiprocessing

      缺陷:多个进程之间通信成本高,切换开销大。

    • 多线程:密集I/O任务(网络I/O,磁盘I/O,数据库I/O)使用多线程合适。

      threading.Thread、multiprocessing.dummy

      缺陷:同一个时间切片只能运行一个线程,不能做到高并行,但是可以做到高并发。

    • 多线程请求返回是无序的,那个线程有数据返回就处理那个线程,而协程返回的数据是有序的。

      缺陷:单线程执行,处理密集CPU和本地磁盘IO的时候,性能较低。处理网络I/O性能还是比较高.

  • 什么是并行,什么是并发?

    并行是同一时间在多个cpu上同时执行多个程序

    并发是同一时间在同一个cpu上在不同程序之间切换运行

  • 请解释同步和异步这两个概念?

    同步执行程序需要等待结果

    异步执行程序不需要等待结果

  • 请谈谈对异步非阻塞的了解?

    执行效率高,可以实现高并发的应用场景

  • 简述信号量的实现原理
    • 信号量机制即利用pv操作来对信号量进行处理。什么是信号量?信号量(semaphore)的数据结构为一个值和一个指针,指针指向等待该信号量的下一个进程。信号量的值与相应资源的使用情况有关。当它的值大于0时,表示当前可用资源的数量;
    • 当它的值小于0时,其绝对值表示等待使用该资源的进程个数。注意,信号量的值仅能由PV操作来改变。一般来说,信号量S³0时,
    • S表示可用资源的数量。执行一次P操作意味着请求分配一个单位资源,因此S的值减1;当S<0时,表示已经没有可用资源,
    • 请求者必须等待别的进程释放该类资源,它才能运行下去。而执行一个V操作意味着释放一个单位资源,因此S的值加1;若S£0,
    • 表示有某些进程正在等待该资源,因此要唤醒一个等待状态的进程,使之运行下去。
  • 程序中的阻塞有哪些?给程序带来了哪些影响?

    input,sleep,io 程序会等待阻塞完成才会进入就绪队列,影响程序运行效率

  • 请分别用多进程、多线程、协程实现生产者消费者模型?

多进程

import time
import random
from multiprocessing import Process,Queue

def producer(name,que):
    for i in range(10):
        time.sleep(random.random())
        food = '便便????'
        print('%s 生产了一陀%s' % (name,food))
        que.put(food)

def consumer(name,que):
    while True:
        time.sleep(random.random())
        food = que.get()
        if not food: break
        print('%s 吃了一陀%s' % (name,food))

if __name__ == '__main__':
    p_l = list()
    que = Queue()
    p = Process(target=producer,args=('baoyuan',que))
    p.start()
    p.join()
    c = Process(target=consumer,args=('alex',que,))
    c.start()
    que.put(None)
    c.join()
    print('全部吃完啦。')

多线程

import time
import random
from queue import Queue
from threading import Thread

def producer(name,que):
    for i in range(10):
        time.sleep(random.random())
        food = '便便????'
        print('%s 生产了一陀%s' % (name,food))
        que.put(food)

def consumer(name,que):
    while True:
        time.sleep(random.random())
        food = que.get()
        if not food: break
        print('%s 吃了一陀%s' % (name,food))

if __name__ == '__main__':
    p_l = list()
    que = Queue()
    p = Thread(target=producer,args=('baoyuan',que))
    p.start()
    p.join()
    c = Thread(target=consumer,args=('alex',que,))
    c.start()
    que.put(None)
    c.join()
    print('全部吃完啦。')

协程

from gevent import monkey;monkey.patch_all()
from queue import Queue
import gevent
import random
import time

def producer(name,que):
    for i in range(10):
        time.sleep(random.random())
        food = '便便????'
        print('%s 生产了一陀%s' % (name,food))
        que.put(food)

def consumer(name,que):
    while True:
        time.sleep(random.random())
        food = que.get()
        if not food: break
        print('%s 吃了一陀%s' % (name,food))

if __name__ == '__main__':
    p_l = list()
    que = Queue()
    p = gevent.spawn(producer,'baoyuan',que)
    p.join()
    c = gevent.spawn(consumer,'alex',que)
    que.put(None)
    c.join()
    print('全部吃完啦。')