高性能Python之:Queue,deque,queue对比

Python作为一门脚本语言,有着很多便捷易用的优秀特点,但他也有一个很大的缺陷,就是性能太差,这也是作为脚本语言不可避免的问题,这里我们来学习一些方法,提高Python的性能:

为了大家测试方便,这里同时给了代码的图片版和文字版。

  • queue是多线程中的使用的栈,但是Python 解释器有一个全局解释器锁(PIL),导致每个 Python 进程中最多同时运行一个线程,因此 Python 多线程程序并不能改善程序性能,不能发挥多核系统的优势。

  • multiprocessing.Queue是Python 2.6 引入的用来实现多进程的一种高性能栈。

  • collections.deque是为了高效实现插入和删除操作的双向列表,适合用于队列和栈。

Python虽然不能利用多线程实现多核任务,但可以通过多进程实现多核任务。多个Python进程有各自独立的GIL锁,互不影响。

multiprocessing.Queue

用于多进程:

先来看官方文档:

高性能Python之:Queue,deque,queue对比

from multiprocessing import Pool

deff(x):

returnx*x

if__name__=='__main__':

withPool(5)asp:

print(p.map(f,[1,2,3]))

输出:

[1,4,9]

multiprocessing supports two types of communication channel between processes:

multiprocessing支持两种类型的进程间通信方式queues和pips。


Queues

The Queue class is a near clone of queue.Queue. For example:

Queue是queue.Queue的近似克隆。高性能Python之:Queue,deque,queue对比

from multiprocessing import Process,Queue

deff(q):

q.put([42,None,'hello'])

if__name__=='__main__':

q=Queue()

p=Process(target=f,args=(q,))

p.start()

print(q.get())# prints "[42, None, 'hello']"

p.join()

Queues are thread and process safe.

Queues是进程和线程安全的,也就是说,同一时间,只能由一个进程或者线程操作Queues。


queue

先来看官方文档:

高性能Python之:Queue,deque,queue对比

def worker():

whileTrue:

item=q.get()

ifitem is None:

break

do_work(item)

q.task_done()

q=queue.Queue()

threads=[]

foriinrange(num_worker_threads):

t=threading.Thread(target=worker)

t.start()

threads.append(t)

foritem insource():

q.put(item)

# block until all tasks are done

q.join()

# stop workers

foriinrange(num_worker_threads):

q.put(None)

fortinthreads:

t.join()


collections.deque

先来看官方文档:

有人对比过以上三者的性能,deque作为一种双向队列性能完胜其他两者。高性能Python之:Queue,deque,queue对比

>>>from collections import deque

>>>d=deque('ghi')# make a new deque with three items

>>>forelem ind:# iterate over the deque's elements

...print(elem.upper())

G

H

I

>>>d.append('j')# add a new entry to the right side

>>>d.appendleft('f')# add a new entry to the left side

>>>d# show the representation of the deque

deque(['f','g','h','i','j'])

>>>d.pop()# return and remove the rightmost item

'j'

>>>d.popleft()# return and remove the leftmost item

'f'

>>>list(d)# list the contents of the deque

['g','h','i']

>>>d[0]# peek at leftmost item

'g'

>>>d[-1]# peek at rightmost item

'i'

>>>list(reversed(d))# list the contents of a deque in reverse

['i','h','g']

>>>'h'ind# search the deque

True

>>>d.extend('jkl')# add multiple elements at once

>>>d

deque(['g','h','i','j','k','l'])

>>>d.rotate(1)# right rotation

>>>d

deque(['l','g','h','i','j','k'])

>>>d.rotate(-1)# left rotation

>>>d

deque(['g','h','i','j','k','l'])

>>>deque(reversed(d))# make a new deque in reverse order

deque(['l','k','j','i','h','g'])

>>>d.clear()# empty the deque

>>>d.pop()# cannot pop from an empty deque

Traceback(most recent call last):

File"<pyshell#6>",line1,in-toplevel-

d.pop()

IndexError:pop froman empty deque

>>>d.extendleft('abc')# extendleft() reverses the input order

>>>d

deque(['c','b','a'])