Skip to content

作品投稿時のSlack複数チャンネル投稿を並列化 - #390

Merged
utgwkk merged 4 commits into
mainfrom
claude/parallel-slack-posting-KaSHM
May 17, 2026
Merged

作品投稿時のSlack複数チャンネル投稿を並列化#390
utgwkk merged 4 commits into
mainfrom
claude/parallel-slack-posting-KaSHM

Conversation

@utgwkk

@utgwkk utgwkk commented May 16, 2026

Copy link
Copy Markdown
Member

概要

作品投稿時に複数のSlackチャンネルへ直列で投稿していた処理を、ThreadPoolExecutor を使って並列化しました。

変更の背景

複数チャンネルを指定して投稿する場合、チャンネル数に比例してレスポンスタイムが増加していました。各チャンネルへの投稿は互いに依存しないため、並列化によって待ち時間を短縮できます。

変更内容

api/src/goduploader/graphql/type/mutation/upload_artwork.py

  • concurrent.futures.ThreadPoolExecutor をインポート
  • 直列の for ループを ThreadPoolExecutor + executor.map() に置き換え
# 変更前
for ch_id in channel_ids:
    try:
        share_to_slack(artwork, top_illust.image_path("full"), share_option, ch_id)
    except Exception as e:
        logging.error(e)

# 変更後
image_path = top_illust.image_path("full")

def _post_to_channel(ch_id):
    try:
        share_to_slack(artwork, image_path, share_option, ch_id)
    except Exception as e:
        logging.error(e)

with ThreadPoolExecutor() as executor:
    executor.map(_post_to_channel, channel_ids)

注意点

  • executor.map() はすべてのスレッドの完了を待ってからリターンするため、後続の Twitter 投稿処理への影響はありません
  • チャンネルごとのエラーハンドリング(ログ出力・例外握り潰し)は従来と同様に維持しています
  • SHARE_TO_SLACK_WITH_IMAGE の場合、各チャンネルが個別に Gyazo へ画像をアップロードする挙動は変更前と同じです(並列になるだけ)

https://claude.ai/code/session_013vQq3dTD72krdJ1jDuzjQz


Generated by Claude Code

claude added 2 commits May 16, 2026 15:02
ThreadPoolExecutorを使用してチャンネルへの投稿を並列実行し、
複数チャンネル指定時のレスポンスタイムを改善する。

https://claude.ai/code/session_013vQq3dTD72krdJ1jDuzjQz
ThreadPoolExecutorのワーカースレッドに入る前にメインスレッドで
SQLAlchemyのリレーション(account・tags)をプリロードする。
commitによりオブジェクトが期限切れになった状態でスレッドが
属性にアクセスするとSQLiteのクロススレッド制限に違反するため。

https://claude.ai/code/session_013vQq3dTD72krdJ1jDuzjQz
Comment on lines +122 to +126
# commit後にSQLAlchemyが全オブジェクトを期限切れにするため、スレッドプールに入る前に
# メインスレッドでリレーションを読み込んでおく(SQLiteのクロススレッドアクセス禁止対策)
_ = artwork.title
_ = artwork.account.name
_ = [t.name for t in artwork.tags]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

さすがにあんまりなので、share_to_slackartwork そのものではなく必要な情報を列挙した引数 (あるいはDTO) を渡すような形にしてほしいナー

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あとは、そもそも並列化する前の関数の呼び出し元でデータを予め用意しておく形になるのであれば、Gyazoへの画像のアップロードも最初の1回だけにするのが収まりよさそう

claude added 2 commits May 16, 2026 15:13
ArtworkSlackInfo/TagSlackInfoデータクラスを導入し、share_to_slack・
upload_imageがORMオブジェクトに依存しない設計に変更。
呼び出し側でcommit直後にDTOを構築することで、スレッドプール内での
SQLiteクロススレッドアクセス問題も根本的に解消する。

https://claude.ai/code/session_013vQq3dTD72krdJ1jDuzjQz
複数チャンネルへ投稿する場合でもGyazoアップロードは1回で済むよう、
upload_imageの呼び出しをスレッドプールの前に移動した。
アップロード済みURLをArtworkSlackInfoのimage_urlフィールドで渡し、
share_to_slackはimage_pathを受け取らない設計に変更。

https://claude.ai/code/session_013vQq3dTD72krdJ1jDuzjQz
@utgwkk
utgwkk merged commit 2c91d23 into main May 17, 2026
2 checks passed
@utgwkk
utgwkk deleted the claude/parallel-slack-posting-KaSHM branch May 17, 2026 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants