python中多線程,多進程,隊列筆記(一)

來源:互聯網
上載者:User

標籤:引入   資料   i/o   with   model   last   需要   was   -o   

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中多線程,多進程,隊列筆記(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.