Python subprcess stdout, stderrをキャプチャする

threading https://ja.stackoverflow.com/questions/60539/subprocess-popenのstdoutとstderrをリアルタイムに取得する # python 3.9 import subprocess from threading import Thread def main(): command = [ 'mycommand', '-opt1', ] proc = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) def read_stdout(stdout): for line in stdout: line_text = line.decode('utf-8').strip() print(f'STDOUT: {line_text}', flush=True) print('stdout closed') # closed when process exited def read_stderr(stderr): for line in stderr: line_text = line.decode('utf-8').strip() print(f'STDERR: {line_text}', flush=True) print('stderr closed') # closed when process exited Thread(target=read_stdout, args=(proc.stdout,)).start() Thread(target=read_stderr, args=(proc.stderr,)).start() while True: returncode = proc.poll() if returncode is not None: break time.sleep(0.01) print(f'exited {returncode}') asyncio https://stackoverflow.com/questions/28492103/how-to-combine-python-asyncio-with-threads https://qiita.com/matsui-k20xx/items/4d1c00c4eefd60ba635b import time import asyncio from concurrent.futures import ThreadPoolExecutor async def main(): loop = asyncio.get_event_loop() executor = ThreadPoolExecutor(2) def operation(): time.sleep(1) print('aaa', flush=True) # 2個ずつ実行される(asyncio.runの実行終了まで2秒かかる) loop.run_in_executor(executor, operation) loop.run_in_executor(executor, operation) loop.run_in_executor(executor, operation) loop.run_in_executor(executor, operation) # main関数の実行はブロックされない print('before aaa') asyncio.run(main()) import time import asyncio from asyncio.subprocess import create_subprocess_exec, PIPE from concurrent.futures import ThreadPoolExecutor async def main(): command = [ 'mycommand', '-opt1', ] proc = await create_subprocess_exec( command[0], *command[1:], stdout=PIPE, stderr=PIPE, ) loop = asyncio.get_event_loop() executor = ThreadPoolExecutor() def read_stdout(stdout): while True: line = asyncio.run_coroutine_threadsafe(stdout.readline(), loop).result() if not line: break line_text = line.decode('utf-8').strip() print(f'STDOUT: {line_text}', flush=True) print('stdout closed') # closed when process exited def read_stderr(stderr): while True: line = asyncio.run_coroutine_threadsafe(stderr.readline(), loop).result() if not line: break line_text = line.decode('utf-8').strip() print(f'STDERR: {line_text}', flush=True) print('stderr closed') # closed when process exited loop.run_in_executor(executor, read_stdout, proc.stdout) loop.run_in_executor(executor, read_stderr, proc.stderr) await proc.wait() print(f'exited {proc.returncode}') https://stackoverflow.com/questions/45600579/asyncio-event-loop-is-closed-when-getting-loop 終了時にEvent loop is closedというエラーが出ることがある? ...

2022年6月6日 · aoirint

DRM・スクリーンショット・Chrome・Firefox

個人的な参考資料として某DRM付きサイトのスクリーンショットを撮ろうとしたんだけど、Chromeは撮らせてくれなかった(Windows 10) Firefoxは撮らせてくれた それとWindowsの絵文字(Segoe UI Emoji)よりTwitter(Twemoji)とかAndroid(Noto Color Emoji)とかの方が好きなんだよなぁ https://lets-emoji.com/emojilist/emojilist-1/ https://lets-emoji.com/cat-face-emoji/ https://docs.microsoft.com/ja-jp/typography/font-list/segoe-ui-emoji Windows 10でNoto Emoji(Androidの絵文字)を使ってみた | 真我生活〜realmeガジェットライフ〜 https://ameblo.jp/nattolecats/entry-12325498414.html またFirefoxに戻るかー なんでFirefoxからChromeに乗り換えたかって、プロファイル切り替えが面倒なのよね ふだん4つのプロファイル(4つのGoogleアカウント)を使い分けてるんだけど、Firefoxのプロファイル選択UI使いにくいんだ… firefox -P これはさあ… Chrome使いたくなるのよ 一応いまもメンテナンスされているProfile Switcherはあるんだけど、ネイティブプログラムのインストール必要だし… https://github.com/null-dev/firefox-profile-switcher こういうタスクバーの細かい気遣い助かる デスクトップアイコンを使わない(デスクトップアイコン非表示)人間なのも影響してる? Firefoxのprofileを切り替えて、集中用のブラウザを用意する - 歩いたら休め https://kiito.hatenablog.com/entry/2020/08/22/183251 Firefox 67以降のユーザープロファイルの仕様の詳細 - 2019-06-14 - ククログ https://www.clear-code.com/blog/2019/6/14.html FirefoxのProfileは仕様としてChromeとは違う道を行ってるみたいだね Vivaldiとか使ったら解消したりしないのかな… https://vivaldi.com/ja/ https://vivaldi.com/ja/blog/vivaldi-business-model/ うーん、アカウント運用を見直した方がいいのかな…

2022年6月6日 · aoirint

VODのHLS配信に関するメモ

https://blog.foresta.me/posts/convert_mp4_to_hls_with_ffmpeg/ https://yosshi.snowdrop.asia/2015/07/27/1156/ https://ffmpeg.org/ffmpeg-formats.html ffmpeg -i video.mp4 -f hls -hls_time 9 -hls_playlist_type vod -hls_segment_filename "stream%3d.ts" stream.m3u8 ffmpeg -i video.mp4 -c:v libx264 -c:a aac -f hls -hls_time 9 -hls_playlist_type vod -hls_segment_filename "stream%3d.ts" stream.m3u8 https://paulownia.hatenablog.com/entry/2020/10/18/163104 https://hub.docker.com/_/nginx default.conf.template server { root /webroot; location ~* \.ts$ { types { video/MP2T ts; } } location ~* \.m3u8$ { types { application/vnd.apple.mpegurl m3u8; # application/x-mpegURL m3u8; } } } niconico: duration: 6s, application/vnd.apple.mpegurl, video/MP2T (durationはキーフレームとかいろいろで勝手に変わるかも?) ffmpeg -i video.mp4 \ -acodec copy \ -vcodec copy \ -vbsf h264_mp4toannexb \ -map 0 \ -f segment \ -segment_format mpegts \ -segment_time 30 \ -segment_list stream.m3u8 \ -segment_list_flags \ -cache stream%03d.ts codecをコピーすると1ファイルのtsに出力されてしまう場合がある libx264で再エンコードする ...

2022年6月6日 · aoirint

Nextcloud 23から24へアップデート

23系で最新23.0.5に更新 docker-compose pull # nextcloud:23 docker-compose exec -u www-data app php occ db:add-missing-indices docker-compose exec -u www-data app php occ maintenance:repair docker-compose exec db mysql_upgrade -p drawioアプリの互換性 https://github.com/pawelrojek/nextcloud-drawio/issues/177 docker-compose pull # nextcloud:24 docker-compose exec -u www-data app php occ db:add-missing-indices docker-compose exec -u www-data app php occ maintenance:repair docker-compose exec db mysql_upgrade -p

2022年5月29日 · aoirint

Pythonパッケージ中でパッケージに同梱したファイルを読み込む

Python 3.7以降の場合、標準モジュールimportlib.resourcesが利用できる。 https://docs.python.org/ja/3/library/importlib.html#module-importlib.resources 以下のようなディレクトリ構造でファイルを同梱する。 setup.py MANIFEST.in README.md LICENSE mymodule __init__.py mymodule.py mydirectory __init__.py myfile1.txt myfile2.bin 注意点 読み込むファイルのあるディレクトリに__init__.pyを作成する MANIFEST.inでファイルがパッケージに同梱されるように記述する include README.md include LICENSE recursive-include mymodule/mydirectory * import importlib.resources as ILR # テキストファイル ILR.read_text('mymodule.mydirectory', 'myfile1.txt', encoding='utf-8') # -> str # バイナリファイル ILR.read_binary('mymodule.mydirectory', 'myfile2.bin') # -> bytes

2022年5月27日 · aoirint

Epic Gamesのディスプレイネームを変更する

公式ヘルプ: https://www.epicgames.com/help/ja/epic-c74/c79/epic-games-a3260 1度変更すると2週間再変更できないので注意。 以下のリンクから「アカウント情報」>「ディスプレイネーム」で変更できる。 登録した時の氏名やメールアドレスの一部が表示されるので、配信中などに変更しないように注意。 https://www.epicgames.com/account/personal

2022年5月26日 · aoirint

JSONファイルに記録したLocal Naiveな時刻をUTC Awareに一括変換するPythonスクリプトのスニペット

https://gist.github.com/aoirint/c231ab43436ce6c3aafe2097dbb77f0e クローラの時刻情報を一度Local Naiveで保存してしまったのでUTC Awareに変換するために作成した (まあ変換処理事故って何度もデータ飛ばしてしまったんだけど) https://twitter.com/aoirint/status/1524287849759408129

2022年5月22日 · aoirint

蛍の光 音楽 素材

笛 栗コーダーカルテット 蛍の光: https://www.amazon.co.jp/dp/B086JDRZ5Z 素材 ピアノ ポケットサウンド 蛍の光(ピアノ): https://pocket-se.info/archives/1628/ AudioStock 「蛍の光」 ピアノ伴奏: https://audiostock.jp/audio/98533 素材 リコーダー ポケットサウンド 蛍の光(リコーダー): https://pocket-se.info/archives/1695/ 素材 ハーモニカ https://audiostock.jp/audio/307963 https://otologic.jp/free/bgm/music_box-event-sotsugyo01.html 新しい蛍の光を見つける Google検索: https://www.google.com/search?q=蛍の光+フリー音源 AudioStock 蛍の光 一覧: https://audiostock.jp/bgm/968

2022年5月22日 · aoirint

PIDから対応するdockerコンテナを見つける

docker-pidパッケージが便利 https://www.glia-computing.com/blog/listing-process-ids-on-docker-containers/ https://github.com/masatanish/docker-pid pip3 install docker==5.0.3 docker-pid==0.0.3

2022年5月22日 · aoirint

topコマンドの設定

shift+wで設定を保存 https://unix.stackexchange.com/questions/424486/linux-command-top-saving-configuration shift+mでメモリ使用率順にソート fから項目にSWAPを追加 https://beyondjapan.com/blog/2016/02/change-sort-top/ https://qiita.com/k0kubun/items/7368c323d90f24a00c2f eで表部分のメモリ量スケールを変更 shift+eでサマリー部分のメモリ量スケールを変更

2022年5月22日 · aoirint