Skip Navigation
Show nav
Dev Center
  • Get Started
  • ドキュメント
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
    • .NET
  • ドキュメント
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku のアーキテクチャ
    • Dyno (アプリコンテナ)
      • Dyno Management
      • Dyno Concepts
      • Dyno Behavior
      • Dyno Reference
      • Dyno Troubleshooting
    • スタック (オペレーティングシステムイメージ)
    • ネットワーキングと DNS
    • プラットフォームポリシー
    • プラットフォームの原則
  • Developer Tools
    • コマンドライン
    • Heroku VS Code Extension
  • デプロイ
    • Git を使用したデプロイ
    • Docker によるデプロイ
    • デプロイ統合
  • 継続的デリバリーとインテグレーション
    • 継続的統合
  • 言語サポート
    • Node.js
      • Working with Node.js
      • Node.js Behavior in Heroku
      • Troubleshooting Node.js Apps
    • Ruby
      • Rails のサポート
      • Bundler の使用
      • Working with Ruby
      • Ruby Behavior in Heroku
      • Troubleshooting Ruby Apps
    • Python
      • Working with Python
      • Python でのバックグランドジョブ
      • Python Behavior in Heroku
      • Django の使用
    • Java
      • Java Behavior in Heroku
      • Working with Java
      • Maven の使用
      • Spring Boot の使用
      • Troubleshooting Java Apps
    • PHP
      • PHP Behavior in Heroku
      • Working with PHP
    • Go
      • Go の依存関係管理
    • Scala
    • Clojure
    • .NET
      • Working with .NET
  • データベースとデータ管理
    • Heroku Postgres
      • Postgres の基礎
      • Postgres スターターガイド
      • Postgres のパフォーマンス
      • Postgres のデータ転送と保持
      • Postgres の可用性
      • Postgres の特別なトピック
      • Migrating to Heroku Postgres
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • その他のデータストア
  • AI
    • Working with AI
    • Heroku Inference
      • Inference API
      • Quick Start Guides
      • AI Models
      • Inference Essentials
    • Vector Database
    • Model Context Protocol
  • モニタリングとメトリクス
    • ログ記録
  • アプリのパフォーマンス
  • アドオン
    • すべてのアドオン
  • 共同作業
  • セキュリティ
    • アプリのセキュリティ
    • ID と認証
      • シングルサインオン (SSO)
    • Private Space
      • インフラストラクチャネットワーキング
    • コンプライアンス
  • Heroku Enterprise
    • Enterprise Accounts
    • Enterprise Team
    • Heroku Connect (Salesforce 同期)
      • Heroku Connect の管理
      • Heroku Connect のリファレンス
      • Heroku Connect のトラブルシューティング
  • パターンとベストプラクティス
  • Heroku の拡張
    • Platform API
    • アプリの Webhook
    • Heroku Labs
    • アドオンのビルド
      • アドオン開発のタスク
      • アドオン API
      • アドオンのガイドラインと要件
    • CLI プラグインのビルド
    • 開発ビルドパック
    • Dev Center
  • アカウントと請求
  • トラブルシューティングとサポート
  • Salesforce とのインテグレーション
  • 言語サポート
  • Python
  • Python でのバックグランドジョブ
  • RQ を使用した Python でのバックグラウンドタスク

RQ を使用した Python でのバックグラウンドタスク

日本語 — Switch to English

この記事の英語版に更新があります。ご覧の翻訳には含まれていない変更点があるかもしれません。

最終更新日 2022年07月18日(月)

Table of Contents

  • 設定
  • ワーカーを作成する
  • ジョブのキューイング
  • デプロイ
  • トラブルシューティング

RQ (Redis Queue)​ により、Heroku 上の Python アプリケーションにバックグラウンドタスク​を追加することが簡単になります。RQ は、Redis データベースをキューとして使用してバックグラウンドジョブを処理します。RQ の使用を開始するには、アプリケーションを設定した後、そのアプリケーションで ワーカープロセスを実行する必要があります。

設定

RQ とその依存関係を設定するには、Pip を使用してそれをインストールします。

$ pip install rq

必ず、requirements.txt​ ファイルにも rq​ を追加してください。

ワーカーを作成する

これでワーカープロセスの作成に必要なすべてのものが用意されたので、作成を開始しましょう。

worker.py​ と呼ばれるファイルを作成します。このモジュールは、受信された、キューに入れられたタスクやプロセスをリッスンします。

import os

import redis
from rq import Worker, Queue, Connection

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDIS_URL', 'redis://localhost:6379')

conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

ここで、新しいワーカープロセスを実行できます。

$ python worker.py

ジョブのキューイング

新しい Redis Queue にジョブを送信するには、コードで Redis にジョブを送信する必要があります。外部モジュール utils.py​ 内にブロック関数があるとします。

import requests

def count_words_at_url(url):
    resp = requests.get(url)
    return len(resp.text.split())

アプリケーションで、RQ キューを作成します。

from rq import Queue
from worker import conn

q = Queue(connection=conn)

次に、関数呼び出しをキューに入れます。

from utils import count_words_at_url

result = q.enqueue(count_words_at_url, 'http://heroku.com')

このブロック関数は、バックグラウンドワーカープロセスで自動的に実行されます。

デプロイ

Heroku に新しいワーカーシステムをデプロイするには、そのワーカープロセスをプロジェクトのルートにある Procfile​ に追加する必要があります。

worker: python worker.py

さらに、Heroku Data for Redis (heroku-redis) アドオンで Redis のインスタンスをプロビジョニングし、git push​ を使用してデプロイします。

$ heroku addons:create heroku-redis
----> Adding heroku-redis to example-app... done, v10 (free)

$ git push heroku main
Counting objects: 5, done.
Delta compression using up to 4 threads.
...

すべてがプッシュされたら、ニーズに従ってワーカーをスケーリングします。

$ heroku scale worker=1
Scaling worker processes... done, now running 1

トラブルシューティング

-p​ フラグと worker​ プロセスタイプの名前でログをフィルタリングしてワーカープロセスの出力を表示します。

$ heroku logs -t -p worker

さらに分離させるために手動でワーカープロセスを起動できます。

$ heroku run worker
Running worker attached to terminal... up, run.1

関連カテゴリー

  • Python でのバックグランドジョブ

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure
  • .NET

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing
  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Github
  • LinkedIn
  • © 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
  • heroku.com
  • Legal
  • Terms of Service
  • Privacy Information
  • Responsible Disclosure
  • Trust
  • Contact
  • Cookie Preferences
  • Your Privacy Choices