Python樹林– category –
-
Python樹林
【Python】Sleeping inside Coroutines: asyncio.sleep
Overview To pause a process for a specific time inside an asynchronous function (coroutine), use await asyncio.sleep(seconds). Unlike the standard time.sleep(), it returns control to the event loop while waiting. This allows other concur... -
Python樹林
【Python】コルーチン内で処理をスリープする:asyncio.sleep
概要 非同期関数(コルーチン)の中で、処理を一定時間停止させたい場合は await asyncio.sleep(秒数) を使用します。 通常の time.sleep() と異なり、待機している間、イベントループに制御を返すため、その間に他の並行タスクを実行できる(ノンブロッキ... -
Python樹林
【Python】Executing Coroutines in an Event Loop: asyncio.run()
Overview This article explains the asyncio.run() function, which serves as the entry point for running defined coroutines (async def) in Python's asynchronous processing (asyncio). This function is a high-level API that manages the creat... -
Python樹林
【Python】イベントループでコルーチンを実行する:asyncio.run()
概要 Pythonの非同期処理(asyncio)において、定義したコルーチン(async def)を実際に動作させるためのエントリーポイントとなる asyncio.run() 関数について解説します。 この関数は、イベントループの作成、コルーチンの実行、そして終了処理(ループ... -
Python樹林
【Python】Defining Asynchronous Functions (Coroutines) and Coroutine Objects
Overview This article explains the basic syntax for defining asynchronous functions using async def and the special return value called a "coroutine object." Unlike regular functions (synchronous functions), a function defined with async... -
Python樹林
【Python】非同期関数(コルーチン)の定義とコルーチンオブジェクト
概要 Pythonで非同期処理を行うための関数定義 async def と、その関数を呼び出した際に生成される特殊な戻り値「コルーチンオブジェクト」について解説します。 通常の関数(同期関数)とは異なり、async def で定義された関数は、呼び出しただけでは中の... -
Python樹林
【Python】Basics of Implementing Asynchronous Processing with async/await
Overview This article explains the basic way to write "Asynchronous Processing" using Python's asyncio module. Traditional synchronous processing (using time.sleep) stops the entire program until a task is finished. By using asynchronous... -
Python樹林
【Python】async/awaitで非同期処理を実装する基本
概要 Pythonの asyncio モジュールを使用した「非同期処理(Asynchronous Processing)」の基本的な書き方を解説します。 従来の同期処理(time.sleep 等)は処理が終わるまでその場でプログラム全体が止まってしまいますが、非同期処理(await asyncio.sl... -
Python樹林
【Python】Using Timeouts with Thread-Safe Queues
Overview By default, the put() and get() methods of queue.Queue wait indefinitely (block) until the operation is completed. By specifying the timeout argument, you can raise an exception (Full or Empty) if the operation does not complete... -
Python樹林
【Python】スレッドセーフなキューでタイムアウトを使用する
概要 queue.Queue の put() と get() は、デフォルトでは処理が完了するまで「無限に待機(ブロック)」します。 timeout 引数を指定することで、一定時間待っても処理が完了しない場合に例外(Full または Empty)を発生させ、プログラムがフリーズするの... -
Python樹林
【Python】Safely Passing Data with Thread-Safe Queues
Overview The queue module in Python provides a mechanism to safely exchange (communicate) data between threads in multi-threaded programming. If you operate standard data structures like a list across multiple threads simultaneously, dat... -
Python樹林
【Python】スレッドセーフなキューでデータを安全に受け渡す
概要 Pythonの queue モジュールは、マルチスレッドプログラミングにおいて、スレッド間でデータを安全にやり取り(通信)するための仕組みを提供します。 list などの通常のデータ構造を複数のスレッドで同時に操作するとデータが破損する恐れがあります... -
Python樹林
【Python】Thread Exclusive Control: Preventing Contention with threading.Lock
Overview In multithreading, when multiple threads try to modify the same variable or resource (files, databases, global variables, etc.) at the same time, data can lose consistency. This is called a "Race Condition." To prevent this, we ... -
Python樹林
【Python】スレッドの排他制御:threading.Lockで競合を防ぐ
概要 マルチスレッド処理において、複数のスレッドが同じ変数やリソース(ファイル、データベース、グローバル変数など)を同時に書き換えようとすると、データが整合性を失う「競合状態(Race Condition)」が発生します。 これを防ぐために threading.Lo... -
Python樹林
【Python】Creating Custom Thread Classes by Inheriting the Thread Class
Overview This article explains how to implement multithreading by inheriting (subclassing) the Thread class in the Python threading module. Compared to simply passing a function to the target argument, this approach is effective when you... -
Python樹林
【Python】Threadクラスを継承して独自の処理スレッドを作る
概要 Pythonの threading モジュールでマルチスレッド処理を実装する際、Thread クラスを継承(サブクラス化)する方法を解説します。 単に関数を target に指定する方法と比較して、スレッド自体に独自のデータ(状態)を持たせたり、複雑なロジックをク... -
Python樹林
【Python】Checking if a Thread is Running or Finished with is_alive()
Overview This article explains how to check whether a thread created with Python's threading module is currently running (alive) or has already finished its task. By using the is_alive() method, you can control your program to perform ot... -
Python樹林
【Python】スレッドが実行中か確認する:is_aliveメソッドの使い方
概要 Pythonの threading モジュールで作成したスレッドが、現在動作中(生存中)なのか、それとも既に処理を終えているのかを確認する方法を解説します。 is_alive() メソッドを使用することで、スレッドの完了を待ちながら別の処理(進捗表示やログ出力... -
Python樹林
【Python】Implementing Concurrency (Multithreading) with the threading Module
Overview This article explains how to implement multithreading to run multiple processes concurrently using Python's standard threading module. This is a basic technique for efficiently handling tasks that involve waiting times (I/O-boun... -
Python樹林
【Python】threadingモジュールで並行処理(マルチスレッド)を実装する
概要 Python標準の threading モジュールを使用して、複数の処理を並行して実行するマルチスレッドの実装方法を解説します。 API通信やファイルの読み書きなど、待ち時間(I/O待ち)が発生するタスクを効率よく処理するための基礎技術です。引数の渡し方(... -
Python樹林
【Python】Concurrency Basics: Differences and Use Cases for Multithreading, Multiprocessing, and Async/await
Overview This article explains three approaches to making your Python code faster and more efficient: Multithreading, Multiprocessing, and Asynchronous programming (async/await). While all three are technologies for "handling multiple ta... -
Python樹林
【Python】並行処理の基本:マルチスレッド・マルチプロセス・async/awaitの違いと使い分け
概要 Pythonで処理を高速化・効率化するための3つのアプローチ(マルチスレッド、マルチプロセス、非同期処理)について解説します。 これらは「複数のタスクを同時にこなす」ための技術ですが、得意な場面が全く異なります。それぞれの仕組みを理解し、適... -
Python樹林
【Python】Saving Attachments from EmailMessage Objects
Overview This article explains how to extract and save attachments from an EmailMessage object (retrieved via Python's email module) to a local disk. We will cover handling binary files like images and PDFs, as well as correctly re-encod... -
Python樹林
【Python】EmailMessageオブジェクトから添付ファイルを取り出して保存する
概要 Pythonの email モジュールを使用して、受信したメール(EmailMessage オブジェクト)から添付ファイルを抽出し、ローカルディスクに保存する方法を解説します。 画像やPDFなどのバイナリファイルだけでなく、テキスト形式の添付ファイルが自動デコー... -
Python樹林
【Python】Extracting Subject, Sender, and Body from EmailMessage Objects
Overview This article explains how to properly extract header information (Subject, From, To) and the message body (Plain Text or HTML) from an EmailMessage object created by Python's email module. We will introduce a modern method using... -
Python樹林
【Python】EmailMessageオブジェクトから件名・宛先・本文を抽出する
概要 Pythonの email モジュールで生成された EmailMessage オブジェクトから、メールの件名、送信元、宛先などのヘッダー情報と、本文(プレーンテキストやHTML)を適切に取り出す方法を解説します。 特にマルチパート形式(HTMLとテキストが混在するメー... -
Python樹林
【Python】Receiving and Parsing Emails from IMAP Servers
Overview This article explains how to receive emails from an IMAP server using Python's standard imaplib library. We will cover the entire process, including connecting to the server and using the email module to parse received data into... -
Python樹林
【Python】imaplibでメールを受信・取得する:IMAP4_SSLとemailモジュールの連携
概要 Python標準ライブラリの imaplib を使用して、IMAPサーバーからメールを受信・取得する方法を解説します。 単にデータを受信するだけでなく、email モジュールと連携して、件名や本文、送信元アドレスを扱いやすいオブジェクト(EmailMessage型)とし... -
Python樹林
【Python】How to Send Emails with Attachments (Automatic MIME Type Detection)
Overview This article explains how to create and send emails with attachments such as PDFs or images using the standard Python library email.message. We will show a versatile implementation that uses the mimetypes module to automatically... -
Python樹林
【Python】メールにファイルを添付して送信する方法(MIMEタイプ自動判定)
概要 Python標準ライブラリ email.message を使用して、PDFや画像などのファイルを添付したメールを作成・送信する方法を解説します。 添付ファイルのデータ形式(MIMEタイプ)を mimetypes モジュールで自動判定させることで、様々な種類のファイルを汎用...