<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>gevent</title>
	<atom:link href="https://techgrowup.net/tag/gevent/feed/" rel="self" type="application/rss+xml" />
	<link>https://techgrowup.net</link>
	<description>エンジニアを強くする</description>
	<lastBuildDate>Wed, 18 Dec 2024 23:32:30 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://techgrowup.net/wp-content/uploads/2021/05/hp-icon-150x150.png</url>
	<title>gevent</title>
	<link>https://techgrowup.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Python開発入門45 Pythonのgeventライブラリを解説</title>
		<link>https://techgrowup.net/python-gevent/</link>
					<comments>https://techgrowup.net/python-gevent/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[techgrowup]]></dc:creator>
		<pubDate>Wed, 18 Dec 2024 23:32:29 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[プログラミング]]></category>
		<category><![CDATA[gevent]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://techgrowup.net/?p=2226</guid>

					<description><![CDATA[はじめに Pythonで並行処理を行う際、マルチスレッドやマルチプロセスはよく利用されますが、それらにはスレッド間の競合やプロセス間通信の複雑さといった課題があります。これを解決するために登場したのが、軽量なグリーンスレ [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading"><strong>はじめに</strong></h1>



<p class="wp-block-paragraph">Pythonで並行処理を行う際、マルチスレッドやマルチプロセスはよく利用されますが、それらにはスレッド間の競合やプロセス間通信の複雑さといった課題があります。これを解決するために登場したのが、軽量な<strong>グリーンスレッド（Green Threads）を活用するgeventライブラリ</strong>です。</p>



<p class="wp-block-paragraph">geventは、非同期I/Oや並行処理を簡潔に実現するためのPythonライブラリで、高速かつ効率的なプログラムを構築できます。本記事では、geventの基本構造や実装例を通じて、その便利さを解説します。</p>



<h2 class="wp-block-heading"><strong>geventとは</strong></h2>



<h3 class="wp-block-heading"><strong>概要</strong></h3>



<p class="wp-block-paragraph"><strong>gevent</strong>は、Pythonで非同期処理を実現するためのライブラリで、以下のような特徴を持っています：</p>



<ul class="wp-block-list">
<li><strong>グリーンスレッド</strong>を使用して軽量な並行処理を実現。</li>



<li>ネットワーク操作やファイル操作などのI/Oを効率化。</li>



<li><strong>同期的なコードスタイル</strong>で非同期処理を記述可能。</li>
</ul>



<h3 class="wp-block-heading"><strong>geventの主な特徴</strong></h3>



<ol class="wp-block-list">
<li><strong>軽量な並行処理</strong><br>グリーンスレッドを用いて、システムリソースを効率的に使用。</li>



<li><strong>非同期I/Oの自動切り替え</strong><br>geventがI/O操作を自動的に検知して切り替え。</li>



<li><strong>簡潔なコード</strong><br>非同期処理を直感的に記述でき、メンテナンス性が高い。</li>
</ol>



<h2 class="wp-block-heading"><strong>geventのインストールとセットアップ</strong></h2>



<h3 class="wp-block-heading"><strong>geventのインストール</strong></h3>



<p class="wp-block-paragraph">以下のコマンドを使ってgeventをインストールします：</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="pip install gevent" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">pip</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">install</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">gevent</span></span></code></pre></div>



<h3 class="wp-block-heading"><strong>簡単なインストール確認</strong></h3>



<p class="wp-block-paragraph">以下のコードを実行して、インストールが正しく完了したことを確認します：</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="import gevent
print(gevent.__version__)" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> gevent</span></span>
<span class="line"><span style="color: #DCDCAA">print</span><span style="color: #D4D4D4">(gevent.</span><span style="color: #9CDCFE">__version__</span><span style="color: #D4D4D4">)</span></span></code></pre></div>



<h2 class="wp-block-heading"><strong>geventの基本的な使い方</strong></h2>



<p class="wp-block-paragraph">以下に、geventの基本的な概念と使用例を解説します。</p>



<h3 class="wp-block-heading"><strong>グリーンスレッド（Greenlets）</strong></h3>



<p class="wp-block-paragraph">グリーンスレッドは、軽量なスレッドで、CPUのコンテキストスイッチを行わずに効率的な並行処理を実現します。</p>



<h6 class="wp-block-heading"><strong>例：シンプルなGreenletの使用</strong></h6>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="import gevent

def task(name):
    print(f&quot;{name} 開始&quot;)
    gevent.sleep(1)
    print(f&quot;{name} 終了&quot;)

# Greenletを起動
gevent.joinall([
    gevent.spawn(task, &quot;タスク1&quot;),
    gevent.spawn(task, &quot;タスク2&quot;),
    gevent.spawn(task, &quot;タスク3&quot;),
])" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> gevent</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">task</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">name</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">print</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">f</span><span style="color: #CE9178">&quot;</span><span style="color: #569CD6">{</span><span style="color: #D4D4D4">name</span><span style="color: #569CD6">}</span><span style="color: #CE9178"> 開始&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    gevent.sleep(</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">print</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">f</span><span style="color: #CE9178">&quot;</span><span style="color: #569CD6">{</span><span style="color: #D4D4D4">name</span><span style="color: #569CD6">}</span><span style="color: #CE9178"> 終了&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955"># Greenletを起動</span></span>
<span class="line"><span style="color: #D4D4D4">gevent.joinall([</span></span>
<span class="line"><span style="color: #D4D4D4">    gevent.spawn(task, </span><span style="color: #CE9178">&quot;タスク1&quot;</span><span style="color: #D4D4D4">),</span></span>
<span class="line"><span style="color: #D4D4D4">    gevent.spawn(task, </span><span style="color: #CE9178">&quot;タスク2&quot;</span><span style="color: #D4D4D4">),</span></span>
<span class="line"><span style="color: #D4D4D4">    gevent.spawn(task, </span><span style="color: #CE9178">&quot;タスク3&quot;</span><span style="color: #D4D4D4">),</span></span>
<span class="line"><span style="color: #D4D4D4">])</span></span></code></pre></div>



<p class="wp-block-paragraph"><strong>出力例：</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="タスク1 開始
タスク2 開始
タスク3 開始
タスク1 終了
タスク2 終了
タスク3 終了" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">タスク1 開始</span></span>
<span class="line"><span style="color: #D4D4D4">タスク2 開始</span></span>
<span class="line"><span style="color: #D4D4D4">タスク3 開始</span></span>
<span class="line"><span style="color: #D4D4D4">タスク1 終了</span></span>
<span class="line"><span style="color: #D4D4D4">タスク2 終了</span></span>
<span class="line"><span style="color: #D4D4D4">タスク3 終了</span></span></code></pre></div>



<h3 class="wp-block-heading"><strong>非同期I/O操作</strong></h3>



<p class="wp-block-paragraph">geventを使うと、非同期I/Oを簡単に実現できます。</p>



<h6 class="wp-block-heading"><strong>例：URLの非同期取得</strong></h6>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="import gevent
from gevent import monkey
import requests

# I/O操作をパッチ
monkey.patch_all()

def fetch_url(url):
    print(f&quot;開始: {url}&quot;)
    response = requests.get(url)
    print(f&quot;{url} の長さ: {len(response.text)}&quot;)

urls = [&quot;https://example.com&quot;, &quot;https://python.org&quot;, &quot;https://gevent.org&quot;]

# Greenletを使って並行処理
gevent.joinall([gevent.spawn(fetch_url, url) for url in urls])" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> gevent</span></span>
<span class="line"><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> gevent </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> monkey</span></span>
<span class="line"><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> requests</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955"># I/O操作をパッチ</span></span>
<span class="line"><span style="color: #D4D4D4">monkey.patch_all()</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">fetch_url</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">url</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">print</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">f</span><span style="color: #CE9178">&quot;開始: </span><span style="color: #569CD6">{</span><span style="color: #D4D4D4">url</span><span style="color: #569CD6">}</span><span style="color: #CE9178">&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    response = requests.get(url)</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">print</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">f</span><span style="color: #CE9178">&quot;</span><span style="color: #569CD6">{</span><span style="color: #D4D4D4">url</span><span style="color: #569CD6">}</span><span style="color: #CE9178"> の長さ: </span><span style="color: #569CD6">{</span><span style="color: #DCDCAA">len</span><span style="color: #D4D4D4">(response.text)</span><span style="color: #569CD6">}</span><span style="color: #CE9178">&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">urls = [</span><span style="color: #CE9178">&quot;https://example.com&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&quot;https://python.org&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&quot;https://gevent.org&quot;</span><span style="color: #D4D4D4">]</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955"># Greenletを使って並行処理</span></span>
<span class="line"><span style="color: #D4D4D4">gevent.joinall([gevent.spawn(fetch_url, url) </span><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> url </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> urls])</span></span></code></pre></div>



<p class="wp-block-paragraph"><strong>説明：</strong></p>



<ul class="wp-block-list">
<li><strong>monkey.patch_all()</strong><br>標準ライブラリのI/O操作をgevent対応にパッチ適用します。</li>



<li><strong>gevent.spawn</strong><br>Greenletで関数を並行して実行します。</li>
</ul>



<h3 class="wp-block-heading"><strong>グループ操作</strong></h3>



<p class="wp-block-paragraph">複数のタスクをグループ化して管理することも可能です。</p>



<h6 class="wp-block-heading"><strong>例：Greenletグループの使用</strong></h6>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="from gevent.pool import Group

def task(name):
    print(f&quot;{name} 実行中&quot;)
    gevent.sleep(1)

group = Group()
group.add(gevent.spawn(task, &quot;タスク1&quot;))
group.add(gevent.spawn(task, &quot;タスク2&quot;))
group.join()" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> gevent.pool </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> Group</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">task</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">name</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">print</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">f</span><span style="color: #CE9178">&quot;</span><span style="color: #569CD6">{</span><span style="color: #D4D4D4">name</span><span style="color: #569CD6">}</span><span style="color: #CE9178"> 実行中&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    gevent.sleep(</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">group = Group()</span></span>
<span class="line"><span style="color: #D4D4D4">group.add(gevent.spawn(task, </span><span style="color: #CE9178">&quot;タスク1&quot;</span><span style="color: #D4D4D4">))</span></span>
<span class="line"><span style="color: #D4D4D4">group.add(gevent.spawn(task, </span><span style="color: #CE9178">&quot;タスク2&quot;</span><span style="color: #D4D4D4">))</span></span>
<span class="line"><span style="color: #D4D4D4">group.join()</span></span></code></pre></div>



<h2 class="wp-block-heading"><strong>geventの応用例</strong></h2>



<h3 class="wp-block-heading"><strong>スレッドロックの簡略化</strong></h3>



<p class="wp-block-paragraph">geventはスレッドロックが不要で、シンプルなコードで競合を防止できます。</p>



<h6 class="wp-block-heading"><strong>例：スレッドロックなしでの共有リソース操作</strong></h6>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="import gevent

counter = 0

def increment():
    global counter
    for _ in range(1000):
        counter += 1

gevent.joinall([gevent.spawn(increment) for _ in range(5)])
print(f&quot;カウンタの値: {counter}&quot;)" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> gevent</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">counter = </span><span style="color: #B5CEA8">0</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">increment</span><span style="color: #D4D4D4">():</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #569CD6">global</span><span style="color: #D4D4D4"> counter</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> _ </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">range</span><span style="color: #D4D4D4">(</span><span style="color: #B5CEA8">1000</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">        counter += </span><span style="color: #B5CEA8">1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">gevent.joinall([gevent.spawn(increment) </span><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> _ </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">range</span><span style="color: #D4D4D4">(</span><span style="color: #B5CEA8">5</span><span style="color: #D4D4D4">)])</span></span>
<span class="line"><span style="color: #DCDCAA">print</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">f</span><span style="color: #CE9178">&quot;カウンタの値: </span><span style="color: #569CD6">{</span><span style="color: #D4D4D4">counter</span><span style="color: #569CD6">}</span><span style="color: #CE9178">&quot;</span><span style="color: #D4D4D4">)</span></span></code></pre></div>



<h3 class="wp-block-heading"><strong>高負荷Webサーバーの構築</strong></h3>



<p class="wp-block-paragraph">geventを使って、高性能な非同期Webサーバーを構築できます。</p>



<h6 class="wp-block-heading"><strong>例：簡易Webサーバー</strong></h6>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="from gevent.pywsgi import WSGIServer

def app(environ, start_response):
    start_response(&quot;200 OK&quot;, [(&quot;Content-Type&quot;, &quot;text/plain&quot;)])
    return [b&quot;Hello, gevent!&quot;]

server = WSGIServer((&quot;0.0.0.0&quot;, 8000), app)
server.serve_forever()" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> gevent.pywsgi </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> WSGIServer</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">app</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">environ</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">start_response</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    start_response(</span><span style="color: #CE9178">&quot;200 OK&quot;</span><span style="color: #D4D4D4">, [(</span><span style="color: #CE9178">&quot;Content-Type&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&quot;text/plain&quot;</span><span style="color: #D4D4D4">)])</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> [</span><span style="color: #569CD6">b</span><span style="color: #CE9178">&quot;Hello, gevent!&quot;</span><span style="color: #D4D4D4">]</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">server = WSGIServer((</span><span style="color: #CE9178">&quot;0.0.0.0&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">8000</span><span style="color: #D4D4D4">), app)</span></span>
<span class="line"><span style="color: #D4D4D4">server.serve_forever()</span></span></code></pre></div>



<h3 class="wp-block-heading"><strong>タイムアウト処理</strong></h3>



<p class="wp-block-paragraph">タスクにタイムアウトを設定することも可能です。</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="import gevent
from gevent.timeout import Timeout

def long_task():
    gevent.sleep(5)

try:
    with Timeout(3):
        long_task()
except Timeout:
    print(&quot;タイムアウト発生&quot;)" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> gevent</span></span>
<span class="line"><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> gevent.timeout </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> Timeout</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">long_task</span><span style="color: #D4D4D4">():</span></span>
<span class="line"><span style="color: #D4D4D4">    gevent.sleep(</span><span style="color: #B5CEA8">5</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #C586C0">try</span><span style="color: #D4D4D4">:</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">with</span><span style="color: #D4D4D4"> Timeout(</span><span style="color: #B5CEA8">3</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">        long_task()</span></span>
<span class="line"><span style="color: #C586C0">except</span><span style="color: #D4D4D4"> Timeout:</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">print</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;タイムアウト発生&quot;</span><span style="color: #D4D4D4">)</span></span></code></pre></div>



<h2 class="wp-block-heading"><strong>geventを使う際の注意点</strong></h2>



<ol class="wp-block-list">
<li><strong>monkeyパッチの適用範囲</strong><br><code>monkey.patch_all()</code>は便利ですが、既存コードに影響を与える可能性があります。適用範囲に注意してください。</li>



<li><strong>CPUバウンド処理には非対応</strong><br>geventはI/Oバウンドタスクに最適化されており、CPUバウンドタスクでは性能が制限されます。</li>



<li><strong>デバッグが難しい場合がある</strong><br>非同期処理特有のエラーやデバッグの難しさがあるため、ログ出力を活用しましょう。</li>
</ol>



<h2 class="wp-block-heading"><strong>まとめ</strong></h2>



<p class="wp-block-paragraph">geventは、Pythonで非同期処理や並行処理を効率的に実現するための強力なライブラリです。そのシンプルな構文と柔軟な設計により、ネットワークプログラミングや高負荷アプリケーション開発に最適です。この記事を参考に、geventの基本から応用までを習得し、プロジェクトに活用してみてください！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techgrowup.net/python-gevent/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: techgrowup.net @ 2026-05-23 17:59:54 by W3 Total Cache
-->