0Pricing
Flask Academy · レッスン

Celeryをアプリファクトリに組み込む

ブローカーとCeleryアプリを設定します

「Celeryをアプリファクトリに組み込む」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Three Pieces to Wire

To run Celery you need three things: the broker URL, a result backend, and a Celery app object your tasks belong to.

Pick a Broker

Redis is the easiest broker to start with. You point Celery at it with a URL so it knows where to push and pull jobs.

broker_url = "redis://localhost:6379/0"

Add a Result Backend

If you want to read task results later, set a result backend. Redis works fine for both broker and backend in development.

result_backend = "redis://localhost:6379/1"

The Celery App Object

Celery centers on one Celery() instance. Tasks are registered on it, and workers load it to know what to run.

from celery import Celery
celery = Celery(__name__)

Why the Factory Matters

In the app factory pattern, config is loaded inside create_app. Celery should pull its broker URL from that same config, not hardcode it.

Read Config from Flask

Store CELERY settings in your Flask config so dev, test, and prod can differ. Then build Celery from app.config inside the factory.

app.config["CELERY_BROKER_URL"] = "redis://localhost:6379/0"

A make_celery Helper

A common pattern is a make_celery(app) function that reads app.config and returns a configured Celery instance.

def make_celery(app):
    c = Celery(app.import_name)
    c.conf.update(app.config)
    return c

Tasks Need App Context

Tasks often touch the database or current_app, which need an app context. Without it, those calls fail when the worker runs.

Subclass the Task Base

The classic fix wraps each task so it pushes an app context before running. That way tasks can use Flask extensions safely.

Two Processes, One Config

The web process and the worker are separate, but both build Celery from the same config so they agree on the broker and backend.

Start a Worker

You run workers from the command line, pointing at your Celery instance. Then they listen for jobs on the broker.

celery -A app.celery worker --loglevel=info

Quick Check

Tasks frequently use the database.

Recap

Set a broker and backend, build a Celery from app.config inside create_app, and push an app context so tasks can use Flask. ✅

よくある質問

「Celeryをアプリファクトリに組み込む」レッスンは無料ですか?

はい。「Celeryをアプリファクトリに組み込む」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「Celeryをアプリファクトリに組み込む」で何を学びますか?

ブローカーとCeleryアプリを設定します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Celeryをアプリファクトリに組み込む」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. リクエスト外へ処理を移す理由
  2. Celeryをアプリファクトリに組み込む
  3. タスクを定義して呼び出す
  4. 結果を追跡して失敗に対処する
← Flask Academyに戻る