Future

ソースコード:Lib/asyncio/futures.py,Lib/asyncio/base_futures.py


Future オブジェクトは低水準のコールバックベースのコード と高水準の async/await コードとの間を橋渡しします。

Future の関数

asyncio.isfuture(obj)

オブジェクトobj が下記のいずれかであればTrue を返します:

  • asyncio.Future クラスのインスタンス

  • asyncio.Task クラスのインスタンス

  • _asyncio_future_blocking 属性を持った Future 的なオブジェクト

バージョン 3.5 で追加.

asyncio.ensure_future(obj,*,loop=None)

下記のいずれかを返します:

  • objFuture オブジェクト、Task オブジェクト、または Future 的なオブジェクト (isfuture() 関数を使って検査します) である場合にはobj そのもの

  • 引数obj がコルーチンである (iscoroutine() 関数を使って検査します) 場合にはobj をラップしたTask オブジェクト; この場合コルーチンはensure_future() によりスケジュールされます。

  • 引数obj が awaitable である (inspect.isawaitable() 関数を使って検査します) 場合にはobj を await するTask オブジェクト

引数obj が上記のいずれにもあてはまらない場合はTypeError 例外を送出します。

重要

See also thecreate_task() function which is thepreferred way for creating new Tasks.

Save a reference to the result of this function, to avoida task disappearing mid-execution.

バージョン 3.5.1 で変更:この関数はどんなawaitable なオブジェクトでも受け入れるようになりました。

バージョン 3.10 で非推奨:Deprecation warning is emitted ifobj is not a Future-like objectandloop is not specified and there is no running event loop.

asyncio.wrap_future(future,*,loop=None)

concurrent.futures.Future オブジェクトをasyncio.Future オブジェクトでラップします。

バージョン 3.10 で非推奨:Deprecation warning is emitted iffuture is not a Future-like objectandloop is not specified and there is no running event loop.

Future オブジェクト

classasyncio.Future(*,loop=None)

Future は非同期処理の最終結果を表すクラスです。スレッドセーフではありません。

Future is anawaitable object. Coroutines can await onFuture objects until they either have a result or an exceptionset, or until they are cancelled. A Future can be awaited multipletimes and the result is same.

典型的には、 Future は低水準のコールバックベースのコード (たとえば asyncio のtransports を使って実装されたプロトコル) が高水準の async/await と相互運用することを可能にするために利用されます。

経験則は Future オブジェクトをユーザー向けの API であらわに利用しないことです。推奨される Future オブジェクトの生成方法はloop.create_future() を呼び出すことです。これによりイベントループの代替実装は、自身に最適化された Future オブジェクトの実装を合わせて提供することができます。

バージョン 3.7 で変更:contextvars モジュールのサポートを追加。

バージョン 3.10 で非推奨:Deprecation warning is emitted ifloop is not specifiedand there is no running event loop.

result()

Future の結果を返します。

Future が完了 していて、set_result() メソッドにより設定された結果を持っている場合は結果の値を返します。

Future が完了 していて、set_exception() メソッドにより設定された例外を持っている場合はその例外を送出します。

Future がキャンセルされた 場合、このメソッドはCancelledError 例外を送出します。

If the Future's result isn't yet available, this method raisesaInvalidStateError exception.

set_result(result)

Future を完了 とマークし、結果を設定します。

Raises aInvalidStateError error if the Future isalreadydone.

set_exception(exception)

Future を完了 とマークし、例外を設定します。

Raises aInvalidStateError error if the Future isalreadydone.

done()

Future が完了 しているならTrue を返します。

Future はキャンセルされた か、またはset_result() メソッドやset_exception() メソッドの呼び出しにより結果や例外が設定された場合に完了 とみなされます。

cancelled()

Future がキャンセルされた 場合にTrue を返します。

通常の場合、このメソッドは Future に処理結果や例外を設定する前に Future がキャンセルされていない ことを確認するために使用されます:

ifnotfut.cancelled():fut.set_result(42)
add_done_callback(callback,*,context=None)

Future が完了 したときに実行されるコールバックを追加します。

callback は Future オブジェクトだけを引数にとって呼び出されます。

このメソッドが呼び出される時点ですでに Future が完了 している場合、コールバックはloop.call_soon() メソッドによりスケジュールされます。

オプションのキーワード引数context を使って、コールバック*callback* を実行する際のコンテキストcontextvars.Context を設定することができます。コンテキストcontext が指定されない場合は現在のコンテキストが使われます。

コールバックにパラメータを渡すには、次の例のようにfunctools.partial() を使うことができます:

# Call 'print("Future:", fut)' when "fut" is done.fut.add_done_callback(functools.partial(print,"Future:"))

バージョン 3.7 で変更:キーワード引数context が追加されました。詳細はPEP 567 を参照してください。

remove_done_callback(callback)

コールバックリストからcallback を削除します。

削除されたコールバックの数を返します。コールバックが複数回追加されていない限り、通常は1が返ります。

cancel(msg=None)

Future をキャンセルし、コールバックをスケジュールします。

Future がすでに完了 またはキャンセル された場合、False を返します。そうでない場合 Future の状態をキャンセル に変更した上でコールバックをスケジュールし、True を返します。

バージョン 3.9 で変更:Added themsg parameter.

exception()

この Future オブジェクトに設定された例外を返します。

例外 (または例外が設定されていないときはNone) は Future が完了 している場合のみ返されます。

Future がキャンセルされた 場合、このメソッドはCancelledError 例外を送出します。

Future が未完了 の場合、このメソッドはInvalidStateError 例外を送出します。

get_loop()

Future オブジェクトが束縛されているイベントループを返します。

バージョン 3.7 で追加.

この例は Future オブジェクトを生成し、Future に結果を設定するための非同期タスクを生成してスケジュールし、そして Future に結果が設定されるまで待機します:

asyncdefset_after(fut,delay,value):# Sleep for *delay* seconds.awaitasyncio.sleep(delay)# Set *value* as a result of *fut* Future.fut.set_result(value)asyncdefmain():# Get the current event loop.loop=asyncio.get_running_loop()# Create a new Future object.fut=loop.create_future()# Run "set_after()" coroutine in a parallel Task.# We are using the low-level "loop.create_task()" API here because# we already have a reference to the event loop at hand.# Otherwise we could have just used "asyncio.create_task()".loop.create_task(set_after(fut,1,'... world'))print('hello ...')# Wait until *fut* has a result (1 second) and print it.print(awaitfut)asyncio.run(main())

重要

Future オブジェクトはconcurrent.futures.Future を模倣してデザインされました。両者の重要な違いは以下の通りです: