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'])