ポリシー¶
警告
Policies are deprecated and will be removed in Python 3.16.
Users are encouraged to use the asyncio.run() function
or the asyncio.Runner with loop_factory to use
the desired loop implementation.
An event loop policy is a global object used to get and set the current event loop, as well as create new event loops. The default policy can be replaced with built-in alternatives to use different event loop implementations, or substituted by a custom policy that can override these behaviors.
The policy object gets and sets a separate event loop per context. This is per-thread by default, though custom policies could define context differently.
Custom event loop policies can control the behavior of
get_event_loop(), set_event_loop(), and new_event_loop().
ポリシーオブジェクトは AbstractEventLoopPolicy 抽象基底クラスで定義された API を実装しなければなりません。
ポリシーの取得と設定¶
以下の関数は現在のプロセスに対するポリシーの取得や設定をするために使われます:
- asyncio.get_event_loop_policy()¶
- プロセス全体にわたる現在のポリシーを返します。 - バージョン 3.14 で非推奨: The - get_event_loop_policy()function is deprecated and will be removed in Python 3.16.
- asyncio.set_event_loop_policy(policy)¶
- プロセス全体にわたる現在のポリシーを policy に設定します。 - policy が - Noneの場合、デフォルトポリシーが現在のポリシーに戻されます。- バージョン 3.14 で非推奨: The - set_event_loop_policy()function is deprecated and will be removed in Python 3.16.
ポリシーオブジェクト¶
イベントループポリシーの抽象基底クラスは以下のように定義されています:
- class asyncio.AbstractEventLoopPolicy¶
- asyncio ポリシーの抽象基底クラスです。 - get_event_loop()¶
- 現在のコンテキストのイベントループを取得します。 - AbstractEventLoopのインターフェースを実装したイベントループオブジェクトを返します。- このメソッドは - Noneを返してはいけません。- バージョン 3.6 で変更. 
 - set_event_loop(loop)¶
- 現在のコンテキストにイベントループ loop を設定します。 
 - new_event_loop()¶
- 新しいイベントループオブジェクトを生成して返します。 - このメソッドは - Noneを返してはいけません。
 - バージョン 3.14 で非推奨: The - AbstractEventLoopPolicyclass is deprecated and will be removed in Python 3.16.
asyncio は以下の組み込みポリシーを提供します:
- class asyncio.DefaultEventLoopPolicy¶
- デフォルトの asyncio ポリシーです。Unix では - SelectorEventLoop、Windows では- ProactorEventLoopを使います。- デフォルトのポリシーを手動でインストールする必要はありません。 asyncio はデフォルトポリシーを使うように自動的に構成されます。 - バージョン 3.8 で変更: Windows では - ProactorEventLoopがデフォルトで使われるようになりました。- バージョン 3.14 で変更: The - get_event_loop()method of the default asyncio policy now raises a- RuntimeErrorif there is no set event loop.- バージョン 3.14 で非推奨: The - DefaultEventLoopPolicyclass is deprecated and will be removed in Python 3.16.
- class asyncio.WindowsSelectorEventLoopPolicy¶
- SelectorEventLoopイベントループ実装を使った別のイベントループポリシーです。- Availability: Windows. - バージョン 3.14 で非推奨: The - WindowsSelectorEventLoopPolicyclass is deprecated and will be removed in Python 3.16.
- class asyncio.WindowsProactorEventLoopPolicy¶
- ProactorEventLoopイベントループ実装を使った別のイベントループポリシーです。- Availability: Windows. - バージョン 3.14 で非推奨: The - WindowsProactorEventLoopPolicyclass is deprecated and will be removed in Python 3.16.
ポリシーのカスタマイズ¶
新しいイベントループのポリシーを実装するためには、以下に示すように DefaultEventLoopPolicy を継承して振る舞いを変更したいメソッドをオーバーライドすることが推奨されます。:
class MyEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
    def get_event_loop(self):
        """Get the event loop.
        This may be None or an instance of EventLoop.
        """
        loop = super().get_event_loop()
        # Do something with loop ...
        return loop
asyncio.set_event_loop_policy(MyEventLoopPolicy())