国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專(zhuān)欄INFORMATION COLUMN

Python 學(xué)習(xí)筆記 并發(fā) future

lewif / 3246人閱讀

摘要:和類(lèi)是高級(jí)類(lèi),大部分情況下只要學(xué)會(huì)使用即可,無(wú)需關(guān)注其實(shí)現(xiàn)細(xì)節(jié)。類(lèi)與類(lèi)十分相似,只不過(guò)一個(gè)是處理進(jìn)程,一個(gè)是處理線(xiàn)程,可根據(jù)實(shí)際需要選擇。示例運(yùn)行結(jié)果不同機(jī)器運(yùn)行結(jié)果可能不同。

concurrent.futures模塊

該模塊主要特色在于ThreadPoolExecutor 和 ProcessPoolExecutor 類(lèi),這兩個(gè)類(lèi)都繼承自concurrent.futures._base.Executor類(lèi),它們實(shí)現(xiàn)的接口能分別在不同的線(xiàn)程或進(jìn)程中執(zhí)行可調(diào)用的對(duì)象,它們都在內(nèi)部維護(hù)著一個(gè)工作線(xiàn)程或者進(jìn)程池。

ThreadPoolExecutor 和 ProcessPoolExecutor 類(lèi)是高級(jí)類(lèi),大部分情況下只要學(xué)會(huì)使用即可,無(wú)需關(guān)注其實(shí)現(xiàn)細(xì)節(jié)。

####ProcessPoolExecutor 類(lèi)

>class ThreadPoolExecutor(concurrent.futures._base.Executor)

>|  This is an abstract base class for concrete asynchronous executors.

>|  Method resolution order:

>|      ThreadPoolExecutor

 |      concurrent.futures._base.Executor

 |      builtins.object

 |

 |  Methods defined here:

 |

 |  init(self, max_workers=None, thread_name_prefix="")

 |      Initializes a new ThreadPoolExecutor instance.

 |

 |      Args:

 |          max_workers: The maximum number of threads that can be used to

 |              execute the given calls.

 |          thread_name_prefix: An optional name prefix to give our threads.

 |

 |  shutdown(self, wait=True)

 |      Clean-up the resources associated with the Executor.

 |

 |      It is safe to call this method several times. Otherwise, no other

 |      methods can be called after this one.

 |

 |      Args:

 |          wait: If True then shutdown will not return until all running

 |              futures have finished executing and the resources used by the

 |              executor have been reclaimed.

 |

 |  submit(self, fn, *args, **kwargs)

 |      Submits a callable to be executed with the given arguments.

 |

 |      Schedules the callable to be executed as fn(*args, **kwargs) and returns

 |      a Future instance representing the execution of the callable.

 |

 |      Returns:

 |          A Future representing the given call.

 |

 |  ----------------------------------------------------------------------

 |  Methods inherited from concurrent.futures._base.Executor:

 |

 |  enter(self)

 |

 |  exit(self, exc_type, exc_val, exc_tb)

 |

 |  map(self, fn, *iterables, timeout=None, chunksize=1)

 |      Returns an iterator equivalent to map(fn, iter).

 |

 |      Args:

 |          fn: A callable that will take as many arguments as there are

 |              passed iterables.

 |          timeout: The maximum number of seconds to wait. If None, then there

 |              is no limit on the wait time.

 |          chunksize: The size of the chunks the iterable will be broken into

 |              before being passed to a child process. This argument is only

 |              used by ProcessPoolExecutor; it is ignored by

 |              ThreadPoolExecutor.

 |

 |      Returns:

 |          An iterator equivalent to: map(func, *iterables) but the calls may

 |          be evaluated out-of-order.

 |

 |      Raises:

 |          TimeoutError: If the entire result iterator could not be generated

 |              before the given timeout.

 |          Exception: If fn(*args) raises for any values.



初始化可以指定一個(gè)最大進(jìn)程數(shù)作為其參數(shù) max_workers 的值,該值一般無(wú)需指定,默認(rèn)為當(dāng)前運(yùn)行機(jī)器的核心數(shù),可以由os.cpu_count()獲取;類(lèi)中含有方法:

map()方法,與python內(nèi)置方法map() 功能類(lèi)似,也就是映射,參數(shù)為:

一個(gè)可調(diào)用函數(shù) fn

一個(gè)迭代器 iterables

超時(shí)時(shí)長(zhǎng) timeout

塊數(shù)chuncksize 如果大于1, 迭代器會(huì)被分塊處理

---->> 該函數(shù)有一個(gè)特性:其返回結(jié)果與調(diào)用開(kāi)始的順序是一致的;在調(diào)用過(guò)程中不會(huì)產(chǎn)生阻塞,也就是說(shuō)可能前者被調(diào)用執(zhí)行結(jié)束之前,后者被調(diào)用已經(jīng)執(zhí)行結(jié)束了。

如果一定要獲取到所有結(jié)果后再處理,可以選擇采用submit()方法和futures.as_completed函數(shù)結(jié)合使用。

shutdown()方法,清理所有與當(dāng)前執(zhí)行器(executor)相關(guān)的資源

submit() 方法,提交一個(gè)可調(diào)用對(duì)象給fn使用

從concurrent.futures._base.Executor繼承了__enter__() 和 __exit__()方法,這意味著ProcessPoolExecutor 對(duì)象可以用于with 語(yǔ)句。

from concurrent import futures
with futures.ProcessPoolExecutor(max_works=3) as executor:
     executor.map()

ThreadPoolExecutor類(lèi)
class ThreadPoolExecutor(concurrent.futures._base.Executor)

 |  This is an abstract base class for concrete asynchronous executors.

 |

 |  Method resolution order:

 |      ThreadPoolExecutor

 |      concurrent.futures._base.Executor

 |      builtins.object

 |

 |  Methods defined here:

 |

 |  init(self, max_workers=None, thread_name_prefix="")

 |      Initializes a new ThreadPoolExecutor instance.

 |

 |      Args:

 |          max_workers: The maximum number of threads that can be used to

 |              execute the given calls.

 |          thread_name_prefix: An optional name prefix to give our threads.

 |

 |  shutdown(self, wait=True)

 |      Clean-up the resources associated with the Executor.

 |

 |      It is safe to call this method several times. Otherwise, no other

 |      methods can be called after this one.

 |

 |      Args:

 |          wait: If True then shutdown will not return until all running

 |              futures have finished executing and the resources used by the

 |              executor have been reclaimed.

 |

 |  submit(self, fn, *args, **kwargs)

 |      Submits a callable to be executed with the given arguments.

 |

 |      Schedules the callable to be executed as fn(*args, **kwargs) and returns

 |      a Future instance representing the execution of the callable.

 |

 |      Returns:

 |          A Future representing the given call.

 |

 |  ----------------------------------------------------------------------

 |  Methods inherited from concurrent.futures._base.Executor:

 |

 |  enter(self)

 |

 |  exit(self, exc_type, exc_val, exc_tb)

 |

 |  map(self, fn, *iterables, timeout=None, chunksize=1)

 |      Returns an iterator equivalent to map(fn, iter).

 |

 |      Args:

 |          fn: A callable that will take as many arguments as there are

 |              passed iterables.

 |          timeout: The maximum number of seconds to wait. If None, then there

 |              is no limit on the wait time.

 |          chunksize: The size of the chunks the iterable will be broken into

 |              before being passed to a child process. This argument is only

 |              used by ProcessPoolExecutor; it is ignored by

 |              ThreadPoolExecutor.

 |

 |      Returns:

 |          An iterator equivalent to: map(func, *iterables) but the calls may

 |          be evaluated out-of-order.

 |

 |      Raises:

 |          TimeoutError: If the entire result iterator could not be generated

 |              before the given timeout.

 |          Exception: If fn(*args) raises for any values.

與ProcessPoolExecutor 類(lèi)十分相似,只不過(guò)一個(gè)是處理進(jìn)程,一個(gè)是處理線(xiàn)程,可根據(jù)實(shí)際需要選擇。

示例
from time import sleep, strftime
from concurrent import futures


def display(*args):
    print(strftime("[%H:%M:%S]"), end="")
    print(*args)


def loiter(n):
    msg = "{}loiter({}): doing nothing for {}s"
    display(msg.format("	"*n, n, n))
    sleep(n)
    msg = "{}loiter({}): done."
    display(msg.format("	"*n, n))
    return n*10


def main():
    display("Script starting")
    executor = futures.ThreadPoolExecutor(max_workers=3)
    results = executor.map(loiter, range(5))
    display("results:", results)
    display("Waiting for individual results:")
    for i, result in enumerate(results):
        display("result {} : {}".format(i, result))


if __name__ == "__main__":
    main()

運(yùn)行結(jié)果:

[20:32:12]Script starting
[20:32:12]loiter(0): doing nothing for 0s
[20:32:12]loiter(0): done.
[20:32:12]      loiter(1): doing nothing for 1s
[20:32:12]              loiter(2): doing nothing for 2s
[20:32:12]results: .result_iterator at 0x00000246DB21BC50>
[20:32:12]Waiting for individual results:
[20:32:12]                      loiter(3): doing nothing for 3s
[20:32:12]result 0 : 0
[20:32:13]      loiter(1): done.
[20:32:13]                              loiter(4): doing nothing for 4s
[20:32:13]result 1 : 10
[20:32:14]              loiter(2): done.
[20:32:14]result 2 : 20
[20:32:15]                      loiter(3): done.
[20:32:15]result 3 : 30
[20:32:17]                              loiter(4): done.
[20:32:17]result 4 : 40

不同機(jī)器運(yùn)行結(jié)果可能不同。

示例中設(shè)置max_workers=3,所以代碼一開(kāi)始運(yùn)行,則有三個(gè)對(duì)象(0,1,2)被執(zhí)行l(wèi)oiter() 操作; 三秒后,對(duì)象0運(yùn)行結(jié)束,得到結(jié)果result 0之后,對(duì)象3才開(kāi)始被執(zhí)行,同理,對(duì)象4的執(zhí)行時(shí)間在對(duì)象1執(zhí)行結(jié)果result 1打印結(jié)束之后。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/42326.html

相關(guān)文章

  • python并發(fā) 1:使用 futures 處理并發(fā)

    摘要:標(biāo)準(zhǔn)庫(kù)中所有阻塞型函數(shù)都會(huì)釋放,允許其他線(xiàn)程運(yùn)行。如果調(diào)用引發(fā)異常,那么當(dāng)從迭代器檢索其值時(shí),將引發(fā)異常。總結(jié)自版就支持線(xiàn)程了,只不過(guò)是使用線(xiàn)程的最新方式。類(lèi)封裝了模塊的組件,使使用線(xiàn)程變得更加方便。下一篇筆記應(yīng)該是使用處理并發(fā)。 作為Python程序員,平時(shí)很少使用并發(fā)編程,偶爾使用也只需要派生出一批獨(dú)立的線(xiàn)程,然后放到隊(duì)列中,批量執(zhí)行。所以,不夸張的說(shuō),雖然我知道線(xiàn)程、進(jìn)程、并行、...

    Kyxy 評(píng)論0 收藏0
  • Python

    摘要:最近看前端都展開(kāi)了幾場(chǎng)而我大知乎最熱語(yǔ)言還沒(méi)有相關(guān)。有關(guān)書(shū)籍的介紹,大部分截取自是官方介紹。但從開(kāi)始,標(biāo)準(zhǔn)庫(kù)為我們提供了模塊,它提供了和兩個(gè)類(lèi),實(shí)現(xiàn)了對(duì)和的進(jìn)一步抽象,對(duì)編寫(xiě)線(xiàn)程池進(jìn)程池提供了直接的支持。 《流暢的python》閱讀筆記 《流暢的python》是一本適合python進(jìn)階的書(shū), 里面介紹的基本都是高級(jí)的python用法. 對(duì)于初學(xué)python的人來(lái)說(shuō), 基礎(chǔ)大概也就夠用了...

    dailybird 評(píng)論0 收藏0
  • Python基礎(chǔ)之使用期物處理并發(fā)

    摘要:本文重點(diǎn)掌握異步編程的相關(guān)概念了解期物的概念意義和使用方法了解中的阻塞型函數(shù)釋放的特點(diǎn)。一異步編程相關(guān)概念阻塞程序未得到所需計(jì)算資源時(shí)被掛起的狀態(tài)。 導(dǎo)語(yǔ):本文章記錄了本人在學(xué)習(xí)Python基礎(chǔ)之控制流程篇的重點(diǎn)知識(shí)及個(gè)人心得,打算入門(mén)Python的朋友們可以來(lái)一起學(xué)習(xí)并交流。 本文重點(diǎn): 1、掌握異步編程的相關(guān)概念;2、了解期物future的概念、意義和使用方法;3、了解Python...

    asoren 評(píng)論0 收藏0
  • python進(jìn)階筆記【2】 --- 一個(gè)奇怪的 __future__ 庫(kù)

    摘要:正文總所周知,和根本就是兩個(gè)東西,每次因?yàn)檫@個(gè)兼容性的問(wèn)題都會(huì)把自己搞瘋。提供了模塊,把下一個(gè)新版本的特性導(dǎo)入到當(dāng)前版本,于是我們就可以在當(dāng)前版本中測(cè)試一些新版本的特性。傳送門(mén)不多,才個(gè)。 寫(xiě)在前面 我是在學(xué)習(xí)cs231n的assignment3的課程,發(fā)現(xiàn)里面的代碼大量頻繁出現(xiàn)了這個(gè)庫(kù),那我就很奇怪了,為什么有個(gè)future這個(gè)奇怪名字的庫(kù)會(huì)出現(xiàn)呢?到底這個(gè)庫(kù)又有什么用?下面就讓我為...

    Achilles 評(píng)論0 收藏0
  • 這篇博客和你嘮嘮 python 并發(fā),滾雪球?qū)Wpython第四季,第16篇

    摘要:圖片下載屬于操作,比較耗時(shí),基于此,可以利用中的多線(xiàn)程將其實(shí)現(xiàn)。更多精彩滾雪球?qū)W完結(jié)滾雪球?qū)W第二輪完結(jié)滾雪球?qū)W第三輪滾雪球?qū)W番外篇完結(jié) 在 python 編碼過(guò)程中...

    qpwoeiru96 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

lewif

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<