### Enqueuing Jobs with Flask-RQ (Python) Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/start.md Shows how to enqueue a Python callable as a job using the Flask-RQ extension. It illustrates enqueuing to the default queue and a named queue using the `enqueue` method. Requires a callable function and the Flask-RQ instance. ```python def send_password_reset(user_id: int) -> None: ... # the default queue rq.queue.enqueue(send_password_reset, user.id) # a named queue rq.queues["email"].enqueue(send_password_reset, user.id) ``` -------------------------------- ### Initializing Flask-RQ Extension (Python) Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/start.md Demonstrates the standard way to initialize the Flask-RQ extension in a Flask or Quart application. It shows creating an RQ instance and associating it with the application, either directly or via `init_app`. Requires a Flask/Quart app instance. ```python from flask_rq import RQ rq = RQ() rq.init_app(app) # call in the app factory if you're using that pattern ``` -------------------------------- ### Running Flask-RQ Worker (CLI) Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/start.md Provides the command-line interface command to start an RQ worker managed by Flask-RQ. This worker is necessary to process jobs enqueued by the application. Requires the Flask-RQ CLI to be installed and configured. ```shell $ flask rq worker ``` -------------------------------- ### Setting up Redis in GitHub Actions (YAML) Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/testing.md Demonstrates how to use the `shogo82148/actions-setup-redis` GitHub Action to install Redis in a CI workflow. The `auto-start: false` option is used to prevent the action from starting the Redis server, allowing tests to manage the server process themselves. ```yaml steps: # ... - uses: shogo82148/actions-setup-redis@v1 with: auto-start: false # ... ``` -------------------------------- ### Enqueuing Jobs with RQ.job Decorator (Python) Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/start.md Presents an alternative method for enqueuing jobs using the `@rq.job` decorator. Decorating a function adds an `enqueue` method directly to it, simplifying the process and allowing specification of the target queue. Requires the Flask-RQ instance and a callable function. ```python @rq.job(queue="email") def send_password_reset(user_id: int) -> None: ... send_password_reset.enqueue(user.id) ``` -------------------------------- ### Install Flask-RQ using pip Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/index.md Installs the Flask-RQ package from the Python Package Index (PyPI) using the pip package manager. This command should be executed in your terminal or command prompt. ```text $ pip install flask-rq ``` -------------------------------- ### Install Flask-RQ via pip (Shell) Source: https://github.com/pallets-eco/flask-rq/blob/main/README.md Command to install the Flask-RQ package using the pip package manager. ```Shell $ pip install flask-rq ``` -------------------------------- ### Start Flask-RQ Worker (Shell) Source: https://github.com/pallets-eco/flask-rq/blob/main/README.md Command to start an RQ worker process using the Flask CLI, which will process jobs from the queue. ```Shell $ flask rq worker ``` -------------------------------- ### Running Redis Docker Container with Persistence Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/server.md Starts a detached Docker container for Redis with data persistence enabled using a named volume 'redis' and configuring the server to use append-only file saving. ```shell docker run -d --rm -p 6379:6379 --volume redis:/data \ redis:latest redis-server --appendonly yes ``` -------------------------------- ### Running Flask-RQ Worker with Scheduler Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/job.md Provides the command-line instruction to start a Flask-RQ worker process that includes the RQ scheduler functionality, necessary for processing jobs scheduled using `enqueue_at` or `enqueue_in`. ```shell $ flask rq worker --with-scheduler ``` -------------------------------- ### Running Valkey Docker Container Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/server.md Starts a detached Docker container for the latest Valkey image, mapping port 6379 and automatically removing the container on exit. ```shell docker run -d --rm -p 6379:6379 valkey/valkey:latest ``` -------------------------------- ### Running Redis Docker Container Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/server.md Starts a detached Docker container for the latest Redis image, mapping port 6379 and automatically removing the container on exit. ```shell docker run -d --rm -p 6379:6379 redis:latest ``` -------------------------------- ### Running Redict Docker Container Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/server.md Starts a detached Docker container for the latest Redict image, mapping port 6379 and automatically removing the container on exit. ```shell docker run -d --rm -p 6379:6379 registry.redict.io/redict:latest ``` -------------------------------- ### Managing Real Redis Server with Pytest Fixtures (Python) Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/testing.md Provides Pytest fixtures to manage a real Redis server for testing. It includes fixtures to reserve a free port, start and stop the Redis server in a subprocess for the test session, and clear all data after each test using `flushall`. Requires `redis-server` and the `ephemeral-port-reserve` library. ```python import collections.abc as cabc import subprocess import time import ephemeral_port_reserve import pytest from redis import Redis from redis.exceptions import ConnectionError as RedisConnectionError @pytest.fixture(scope="session") def redis_port() -> int: return ephemeral_port_reserve.reserve() # type: ignore[no-any-return] @pytest.fixture(scope="session", autouse=True) def _start_redis( tmp_path_factory: pytest.TempPathFactory, redis_port: int ) -> cabc.Iterator[None]: proc = subprocess.Popen( ["redis-server", "--port", str(redis_port)], cwd=tmp_path_factory.mktemp("redis-server"), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) while True: try: redis = Redis(port=redis_port, single_connection_client=True) redis.ping() break except (ConnectionError, RedisConnectionError): # pragma: no cover time.sleep(0.1) yield proc.terminate() proc.wait() @pytest.fixture(autouse=True) def _reset_redis(redis_port: int) -> cabc.Iterator[None]: yield Redis(port=redis_port, single_connection_client=True).flushall() ``` -------------------------------- ### Calling Sync Enqueue from Async Context with asyncio.to_thread Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/job.md Provides an example of wrapping the synchronous `rq.enqueue` call using `asyncio.to_thread` when calling from an `async def` function, as a potential way to avoid blocking, although typically not necessary. ```python await asyncio.to_thread(rq.enqueue, send_password_reset, user_id=user.id) ``` -------------------------------- ### Initialize Flask-RQ and Define/Enqueue Job (Python) Source: https://github.com/pallets-eco/flask-rq/blob/main/README.md Demonstrates how to initialize Flask-RQ with a Flask application, define a background job using the @rq.job decorator, and enqueue the job from a Flask route. ```Python from flask import Flask, g from flask_rq import RQ app = Flask(__name__) rq = RQ(app) @rq.job def send_password_reset_job(user_id: int) -> None: ... @app.route("/auth/send-password-reset") def send_password_reset(): send_password_reset_job.enqueue(user_id=g.user.id) return "Check your email!" ``` -------------------------------- ### Configuring Flask-RQ with FakeRedis (Python) Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/testing.md Shows how to configure a Flask application using Flask-RQ to use the `fakeredis.FakeRedis` connection class. This replaces the standard Redis connection with an in-memory implementation provided by the FakeRedis library, useful for testing without a running Redis server. ```python app.config["RQ_CONNECTION_CLASS"] = "fakeredis.FakeRedis" ``` -------------------------------- ### Enqueuing Sync and Async Jobs with Flask-RQ Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/job.md Demonstrates how to use the `enqueue` method of a Flask-RQ queue instance to add both synchronous (`def`) and asynchronous (`async def`) Python functions as jobs. Flask-RQ automatically handles app context activation and asyncio integration. ```python def update_stats(data): ... async def send_password_reset(user_id): ... rq.queue.enqueue(update_stats, data=...) rq.queues["email"].enqueue(send_passord_reset, user_id=user.id) ``` -------------------------------- ### Enqueuing Jobs to Default and Named RQ Queues with Flask-RQ Python Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/queue.md Illustrates how to enqueue jobs to both the default queue using rq.queue.enqueue and a specific named queue (e.g., "email") using rq.queues["email"].enqueue. This demonstrates the standard method for adding background jobs to different queues managed by Flask-RQ. ```python rq.queue.enqueue(update_stats, data=...) rq.queues["email"].enqueue(send_passord_reset, user_id=user.id) ``` -------------------------------- ### Using the @rq.job Decorator and Enqueue Method Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/job.md Illustrates the use of the `@rq.job` decorator to wrap a function, adding a type-checked `.enqueue()` method for easier job submission. Shows the decorator definition and how to call the added enqueue method. ```python @rq.job(queue="email") async def send_password_reset(user_id: int) -> None: ... ``` ```python send_password_reset.enqueue(user_id=user.id) ``` ```python await send_password_reset(user_id=user.id) ``` -------------------------------- ### Configuring Multiple RQ Queues with Shared and Separate Redis Connections with Flask-RQ Python Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/queue.md Demonstrates a complex configuration where some queues share the default Redis connection ("default", "priority"), while others use a separate connection ("email") or share that separate connection by reference ("email-priority"). This is achieved using RQ_CONNECTION, RQ_QUEUES, and RQ_QUEUE_CONNECTIONS. ```python RQ_CONNECTION = "redis://redis.my-app.example" RQ_QUEUES = ["priority", "email", "email-priority"] RQ_QUEUE_CONNECTIONS = { "email": "redis://email-redis.my-app.example", "email-priority": "email" } ``` -------------------------------- ### Using the rq.job Decorator to Enqueue Functions with Flask-RQ Python Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/queue.md Shows how to use the @rq.job decorator to wrap a function, automatically adding an enqueue method to it. This method simplifies enqueuing the decorated function to a specified queue (here, "email") using the Flask-RQ extension instance, providing a more concise syntax. ```python @rq.job(queue="email") def send_password_reset(user_id: int) -> None: ... send_password_reset.enqueue(user_id=user.id) # same as rq.queues["email"].enqueue(send_password_reset, user_id=user.id) ``` -------------------------------- ### Configuring Separate Redis Connection for Specific RQ Queue with Flask-RQ Python Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/queue.md Shows how to configure a specific queue (e.g., "email") to use a different Redis connection than the default one by setting the RQ_QUEUE_CONNECTIONS configuration variable. This allows routing jobs for certain queues to a separate Redis instance. ```python RQ_QUEUE_CONNECTIONS = {"email": "redis://email-redis.my-app.example"} ``` -------------------------------- ### Configuring Default RQ Connection URL with Flask-RQ Python Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/queue.md Shows how to configure the default Redis connection URL for Flask-RQ by setting the RQ_CONNECTION configuration variable. This overrides the default connection to localhost and directs all queues (unless specified otherwise) to the provided Redis instance. ```python RQ_CONNECTION = "redis://redis.my-app.example" ``` -------------------------------- ### Configuring Multiple RQ Queues with Flask-RQ Python Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/queue.md Demonstrates how to configure Flask-RQ to use additional named queues besides the default one by setting the RQ_QUEUES configuration variable. This allows jobs to be routed to specific queues for different processing priorities or types. ```python RQ_QUEUES = ["email", "priority"] ``` -------------------------------- ### Overriding Default Queue for Job Enqueue with Flask-RQ Python Source: https://github.com/pallets-eco/flask-rq/blob/main/docs/queue.md Demonstrates how to enqueue a job to a different queue than the one specified in the @rq.job decorator. By directly calling enqueue on a specific queue instance from rq.queues, you can override the default queue configured for the decorated function. ```python rq.queues["priority"].enqueue(send_password_reset, user_id=user.id) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.