python中多线程,多进程,队列笔记

Posted 菜鸟的日常

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中多线程,多进程,队列笔记相关的知识,希望对你有一定的参考价值。

threading简介:
If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously

多线程用在单核cpu上进行IO密集型操作会得到性能提升,多核cpu多线程没有意义,而且得不到性能提升。主要是因为GIL(python的全局解释器锁),导致每个 Python进程中最多同时运行一个线程,因此python多线程程序并不能改善程序性能,不能发挥多核系统的优势。

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

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

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

队列是典型的生产者-消费者模式。
生产者生产货物,然后把货物放到一个队列之类的数据结构中,生产货物所需要话费的时间无法确定,消费者消耗生产者生产的货物时间也不是确定的。
队列在多线程间线程完成安全交换信息时特别有用
Queue模块可以用来进行线程间通讯,让各个线程之间共享数据

下面解释下栈和队列的区别:
1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIFO)的特征

2.队列(queue)是一种具有先进先出特征的线性数据结构,元素的增加只能在一端进行,元素的删除只能在另一端进行。能够增加元素的队列一端称为队尾,可以删除元素的队列一端则称为队首。

下面是官方实例:
Using Lists as Stacks
The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]

Using Lists as Queues

It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).

To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends. For example:

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
‘Eric‘
>>> queue.popleft() # The second to arrive now leaves
‘John‘
>>> queue # Remaining queue in order of arrival
deque([‘Michael‘, ‘Terry‘, ‘Graham‘])































以上是关于python中多线程,多进程,队列笔记的主要内容,如果未能解决你的问题,请参考以下文章

python中多进程/多线程简单介绍

python中多线程

Python中多进程的使用

Python有了asyncio和aiohttp在爬虫这类型IO任务中多线程/多进程还有存在的必要吗?

python中多线程

python中多进程+协程的使用以及为啥要用它