
エントリーの編集

エントリーの編集は全ユーザーに共通の機能です。
必ずガイドラインを一読の上ご利用ください。
Semaphoreを使ってPythonの非同期処理の平行処理数をコントロールする
記事へのコメント0件
- 注目コメント
- 新着コメント
このエントリーにコメントしてみましょう。
注目コメント算出アルゴリズムの一部にLINEヤフー株式会社の「建設的コメント順位付けモデルAPI」を使用しています

- バナー広告なし
- ミュート機能あり
- ダークモード搭載
関連記事
Semaphoreを使ってPythonの非同期処理の平行処理数をコントロールする
import asyncio class AsyncResource: def __init__(self, concurrency: int = 10): self.semaphore = a... import asyncio class AsyncResource: def __init__(self, concurrency: int = 10): self.semaphore = asyncio.Semaphore(concurrency) async def task(self): async with self.semaphore: # 11個目以降のタスクはここでブロックされる print("Hello") await self.call_api() print("World") async def call_api(self): await asyncio.sleep(1) async def main(): async_resource = AsyncResource() tasks = [async_resource.task() for _ in range(10