python的兩個Future區(qū)別

第一個是asyncio.Future,另外一個是concurrent.futures.Future

前者幾乎兼容后者,區(qū)別在于:

  1. asyncio.Future.result,asyncio.Future.exception不存在一個超時選項(xiàng),并且如果被中斷的時候future還沒有結(jié)束,會觸發(fā)一個exception

2.如果回調(diào)函數(shù)用asyncio.Future.add_done_callback()方法注冊了,結(jié)果一定會通過event_loop的asyncio.AbstractEventLoop.call_soon_threadsafe()來返回

  1. asyncio.Future類里面沒有wait()和as_completed()方法(concurrent.futures.Future有)

This class is not thread safe.

concurrent.futures.Future的方法

cancel()
Attempt to cancel the call. If the call is currently being executed and cannot be cancelled then the method will return False, otherwise the call will be cancelled and the method will return True

cancelled
Return True if the call was successfully cancelled.

running
Return True if the call is currently being executed and cannot be cancelled.

done
Return True if the call was successfully cancelled or finished running.

result(timeout=None)
Return the value returned by the call. If the call hasn’t yet completed then this method will wait up to timeoutseconds. If the call hasn’t completed in timeout seconds, then a concurrent.futures.TimeoutError will be raised. timeout can be an int or float. If timeout is not specified or None, there is no limit to the wait time.
If the future is cancelled before completing then CancelledError will be raised.If the call raised, this method will raise the same exception.

exception(timeout=None)
Return the exception raised by the call. If the call hasn’t yet completed then this method will wait up to timeout seconds. If the call hasn’t completed in timeout seconds, then a concurrent.futures.TimeoutErrorwill be raised. timeout can be an int or float. If timeout is not specified or None, there is no limit to the wait time.
If the future is cancelled before completing then CancelledError will be raised.
If the call completed without raising, None is returned.

add_done_callback(fn)
Attaches the callable fn to the future. fn will be called, with the future as its only argument, when the future is cancelled or finishes running.
Added callables are called in the order that they were added and are always called in a thread belonging to the process that added them. If the callable raises an Exception
subclass, it will be logged and ignored. If the callable raises a BaseException
subclass, the behavior is undefined.
If the future has already completed or been cancelled, fn will be called immediately.

The following Future methods are meant for use in unit tests and Executor
implementations.

set_running_or_notify_cancel()
This method should only be called by Executor implementations before executing the work associated with the Future and by unit tests.
If the method returns False then the Future was cancelled, i.e. Future.cancel()
was called and returned True
. Any threads waiting on the Future completing (i.e. through as_completed()
or wait()) will be woken up.
If the method returns True then the Future
was not cancelled and has been put in the running state, i.e. calls to Future.running() will return True.
This method can only be called once and cannot be called after Future.set_result() or Future.set_exception() have been called.

set_result(result)
Sets the result of the work associated with the Future to result.
This method should only be used by Executor implementations and unit tests.

set_exception
(exception)
Sets the result of the work associated with the Future to the Exception exception.
This method should only be used by Executor implementations and unit tests.

asyncio.Future的方法
cancel()
Cancel the future and schedule callbacks.

If the future is already done or cancelled, return False. Otherwise, change the future’s state to cancelled, schedule the callbacks and return True.

cancelled()
Return True if the future was cancelled.

done()
Return True if the future is done.

Done means either that a result / exception are available, or that the future was cancelled.

result()
Return the result this future represents.

If the future has been cancelled, raises CancelledError. If the future’s result isn’t yet available, raises InvalidStateError. If the future is done and has an exception set, this exception is raised.

exception()
Return the exception that was set on this future.

The exception (or None if no exception was set) is returned only if the future is done. If the future has been cancelled, raises CancelledError. If the future isn’t done yet, raises InvalidStateError.

add_done_callback(fn)
Add a callback to be run when the future becomes done.

The callback is called with a single argument - the future object. If the future is already done when this is called, the callback is scheduled with call_soon().

Use functools.partial to pass parameters to the callback. For example, fut.add_done_callback(functools.partial(print, "Future:", flush=True)) will call print("Future:", fut, flush=True).

remove_done_callback(fn)
Remove all instances of a callback from the “call when done” list.

Returns the number of callbacks removed.

set_result(result)
Mark the future done and set its result.

If the future is already done when this method is called, raises InvalidStateError.

set_exception(exception)
Mark the future done and set an exception.

If the future is already done when this method is called, raises InvalidStateError.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末归园,一起剝皮案震驚了整個濱河市截汪,隨后出現(xiàn)的幾起案子河爹,更是在濱河造成了極大的恐慌蚀瘸,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绢掰,死亡現(xiàn)場離奇詭異锄贷,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)曼月,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進(jìn)店門谊却,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人哑芹,你說我怎么就攤上這事炎辨。” “怎么了聪姿?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵碴萧,是天一觀的道長。 經(jīng)常有香客問我末购,道長破喻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任盟榴,我火速辦了婚禮曹质,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘擎场。我一直安慰自己羽德,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布迅办。 她就那樣靜靜地躺著宅静,像睡著了一般。 火紅的嫁衣襯著肌膚如雪站欺。 梳的紋絲不亂的頭發(fā)上姨夹,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天磷账,我揣著相機(jī)與錄音够颠,去河邊找鬼榄鉴。 笑死,一個胖子當(dāng)著我的面吹牛剃诅,可吹牛的內(nèi)容都是我干的矛辕。 我是一名探鬼主播付魔,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼几苍,長吁一口氣:“原來是場噩夢啊……” “哼妻坝!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起厘贼,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤圣拄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后赁遗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體族铆,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年剖煌,在試婚紗的時候發(fā)現(xiàn)自己被綠了逝淹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片栅葡。...
    茶點(diǎn)故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡欣簇,死狀恐怖坯约,靈堂內(nèi)的尸體忽然破棺而出闹丐,到底是詐尸還是另有隱情被因,我是刑警寧澤梨与,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布粥鞋,位于F島的核電站,受9級特大地震影響陷虎,放射性物質(zhì)發(fā)生泄漏尚猿。R本人自食惡果不足惜窝稿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望凿掂。 院中可真熱鬧,春花似錦庄萎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽忍捡。三九已至集漾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間砸脊,已是汗流浹背具篇。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工凌埂, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人埃疫。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓臭笆,卻偏偏與公主長得像叙淌,于是被迫代替她去往敵國和親愁铺。 傳聞我的和親對象是個殘疾皇子茵乱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容