最終的にはワーカープログラムで文字起こしや生成AIのプログラムを起動することを考えます.その第一歩として標準ライブラリである subprocess モジュールを使って,Python コードの中から外部のコマンドを実行してみましょう.まず,watch.py については前のページから変更はありません.ワーカープログラムについては次のとおり編集します.具体的には subprocess をインポートし,echo コマンドを用いて画面に hello watchdog と表示させます.なお,Windows コマンドプロンプトの echo コマンドはこちら,Bash の echo コマンドはこちらを参照してください.また,PowerShell では Write-Output のエイリアス(別名)が echo であることに注意してください.
worker.py
from pathlib import Path
import shutil
import time
import datetime
import subprocess
BASE_DIR = Path(__file__).parent
QUEUE_DIR = BASE_DIR / "queue"
WORKING_DIR = BASE_DIR / "working"
DONE_DIR = BASE_DIR / "done"
# ディレクトリを作っておく
QUEUE_DIR.mkdir(parents=True, exist_ok=True)
WORKING_DIR.mkdir(parents=True, exist_ok=True)
DONE_DIR.mkdir(parents=True, exist_ok=True)
try:
while True:
for src in QUEUE_DIR.glob("*.m*"): # 監視するファイルの拡張子
print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 処理開始: {src.name}")
working_file = WORKING_DIR / src.name
shutil.move(src, working_file)
print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 作業開始: {working_file}")
print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 処理中...")
subprocess.run(["echo", "hello watchdog"])
time.sleep(25)
dst = DONE_DIR / src.name
shutil.move(working_file, dst)
print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 完了: {dst}")
time.sleep(1)
except KeyboardInterrupt:
print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 停止します")
ターミナルを2個起動し,それぞれで watch.py と worker.py を実行します.
(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog> python watch.py ⏎
監視用 watch フォルダに4つのマークダウンファイルをまとめて設置しました.
(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog> python watch.py
[16:43:40.030951] 新しいファイルを検出: C:\Users\rinsaka\Documents\python\watchdog\watch\sample1.md
[16:43:40.031187] 新しいファイルの名前: sample1.md
[16:43:50.032255] ファイルの受取りが完了しました
[16:43:50.032457] ファイルの移動開始: C:\Users\rinsaka\Documents\python\watchdog\queue\sample1.md
[16:43:50.033379] ファイルの移動完了: C:\Users\rinsaka\Documents\python\watchdog\queue\sample1.md
[16:43:50.033607] 新しいファイルを検出: C:\Users\rinsaka\Documents\python\watchdog\watch\sample2.md
[16:43:50.033762] 新しいファイルの名前: sample2.md
[16:44:00.034908] ファイルの受取りが完了しました
[16:44:00.035040] ファイルの移動開始: C:\Users\rinsaka\Documents\python\watchdog\queue\sample2.md
[16:44:00.047893] ファイルの移動完了: C:\Users\rinsaka\Documents\python\watchdog\queue\sample2.md
[16:44:00.048251] 新しいファイルを検出: C:\Users\rinsaka\Documents\python\watchdog\watch\sample3.md
[16:44:00.048562] 新しいファイルの名前: sample3.md
[16:44:10.049526] ファイルの受取りが完了しました
[16:44:10.049801] ファイルの移動開始: C:\Users\rinsaka\Documents\python\watchdog\queue\sample3.md
[16:44:10.050873] ファイルの移動完了: C:\Users\rinsaka\Documents\python\watchdog\queue\sample3.md
[16:44:10.051079] 新しいファイルを検出: C:\Users\rinsaka\Documents\python\watchdog\watch\sample4.md
[16:44:10.051177] 新しいファイルの名前: sample4.md
[16:44:20.052861] ファイルの受取りが完了しました
[16:44:20.053156] ファイルの移動開始: C:\Users\rinsaka\Documents\python\watchdog\queue\sample4.md
[16:44:20.073478] ファイルの移動完了: C:\Users\rinsaka\Documents\python\watchdog\queue\sample4.md
Ctrl + C
[16:45:37.676945] 停止します
(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog>
ワーカープログラムでは「hello watchdog」と表示されていることが分かりました.
(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog> python worker.py [16:43:50.649040] 処理開始: sample1.md [16:43:50.649617] 作業開始: C:\Users\rinsaka\Documents\python\watchdog\working\sample1.md [16:43:50.649762] 処理中... hello watchdog [16:44:15.662985] 完了: C:\Users\rinsaka\Documents\python\watchdog\done\sample1.md [16:44:16.663507] 処理開始: sample2.md [16:44:16.664295] 作業開始: C:\Users\rinsaka\Documents\python\watchdog\working\sample2.md [16:44:16.664362] 処理中... hello watchdog [16:44:41.673980] 完了: C:\Users\rinsaka\Documents\python\watchdog\done\sample2.md [16:44:41.674235] 処理開始: sample3.md [16:44:41.675430] 作業開始: C:\Users\rinsaka\Documents\python\watchdog\working\sample3.md [16:44:41.675557] 処理中... hello watchdog [16:45:06.688436] 完了: C:\Users\rinsaka\Documents\python\watchdog\done\sample3.md [16:45:07.689160] 処理開始: sample4.md [16:45:07.689734] 作業開始: C:\Users\rinsaka\Documents\python\watchdog\working\sample4.md [16:45:07.689833] 処理中... hello watchdog [16:45:32.700563] 完了: C:\Users\rinsaka\Documents\python\watchdog\done\sample4.md Ctrl + C [16:45:38.428107] 停止します (py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog>
このように外部のコマンドが正しく動作することが分かりました.