国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python中利用Future對象異步返回結果示例代碼

2020-02-16 10:11:18
字體:
來源:轉載
供稿:網友

前言

本文主要給大家介紹了關于python中用Future對象異步返回結果的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。

一個Future是用來表示將來要完成的結果,異步循環可以自動完成對這種對象的狀態觸發。

例子如下:

import asyncio   def mark_done(future, result):  print('setting future result to {!r}'.format(result))  future.set_result(result)   event_loop = asyncio.get_event_loop() try:  all_done = asyncio.Future()   print('scheduling mark_done')  event_loop.call_soon(mark_done, all_done, 'the result')   print('entering event loop')  result = event_loop.run_until_complete(all_done)  print('returned result: {!r}'.format(result)) finally:  print('closing event loop')  event_loop.close()  print('future result: {!r}'.format(all_done.result())) 

輸出結果如下:

scheduling mark_doneentering event loopsetting future result to 'the result'returned result: 'the result'closing event loopfuture result: 'the result'

在這個例子里,并沒有調用return語句,但也可以生成一個結果返回。Future的使用跟協程使用是一樣的。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林站長站的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江川县| 高安市| 浦北县| 铜鼓县| 丽水市| 屏边| 山东省| 枝江市| 江门市| 柳州市| 托克逊县| 栖霞市| 阳曲县| 南投县| 上蔡县| 固安县| 双牌县| 黄浦区| 新竹县| 防城港市| 曲沃县| 农安县| 台前县| 兴义市| 道真| 抚顺市| 中超| 南陵县| 衢州市| 舒兰市| 永胜县| 祁阳县| 沿河| 襄汾县| 九江县| 汨罗市| 汝城县| 霍邱县| 阜平县| 藁城市| 鄯善县|