Python の代替非同期ジェネレーター
from asyncio import run, sleep class AlternatingGenerator: def __init__(self, gen1, gen2): self.gen1 = gen1 self.gen2 = gen2 self.next = gen1 def __aiter__(self): return self async def __anext__(self): # if we are done we both generators if not self.gen1 and not self.gen2: raise StopAsyncIteration try: # saving the current generator into a variable for later current = self.next # if the other genrerator is not null, alternating to it for the next iteration if self.next is self.gen1 and self.gen2: self.next = self.gen2 elif self.next is self.gen2 and self.gen1: self.next = self.gen1 # calling the generator of the current iteration and returning the next result return await anext(current) except StopAsyncIteration: # unsetting the generator that just finished if current is self.gen1: self.gen1 = None else: self.gen2 = None # recursive call, trying again with the other generator return await self.__anext__() async def three(): for i in range(3, 31, 10): await sleep(0.25) yield i async def five(): for i in range(5, 101, 10): await sleep(0.25) yield i async def main(): gen = AlternatingGenerator(three(), five()) try: # or just use "async for item in gen:" while True: print(await anext(gen)) except StopAsyncIteration: pass if __name__ == '__main__': run(main())
$ python alternating_generator.py 3 5 13 15 23 25 35 45 55 65 75 85 95
以上がPython の代替非同期ジェネレーターの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









LinuxターミナルでPythonバージョンを表示する際の許可の問題の解決策PythonターミナルでPythonバージョンを表示しようとするとき、Pythonを入力してください...

PythonのPandasライブラリを使用する場合、異なる構造を持つ2つのデータフレーム間で列全体をコピーする方法は一般的な問題です。 2つのデータがあるとします...

10時間以内にコンピューター初心者プログラミングの基本を教える方法は?コンピューター初心者にプログラミングの知識を教えるのに10時間しかない場合、何を教えることを選びますか...

Pythonでは、文字列を介してオブジェクトを動的に作成し、そのメソッドを呼び出す方法は?これは一般的なプログラミング要件です。特に構成または実行する必要がある場合は...

UvicornはどのようにしてHTTPリクエストを継続的に聞きますか? Uvicornは、ASGIに基づく軽量のWebサーバーです。そのコア機能の1つは、HTTPリクエストを聞いて続行することです...

この記事では、numpy、pandas、matplotlib、scikit-learn、tensorflow、django、flask、and requestsなどの人気のあるPythonライブラリについて説明し、科学的コンピューティング、データ分析、視覚化、機械学習、Web開発、Hの使用について説明します。

fiddlereveryversings for the-middleの測定値を使用するときに検出されないようにする方法
