Skip to content

Skip no-op task if possible#7

Merged
pencil merged 3 commits intomainfrom
optimize-noop
Jun 23, 2025
Merged

Skip no-op task if possible#7
pencil merged 3 commits intomainfrom
optimize-noop

Conversation

@pencil
Copy link
Collaborator

@pencil pencil commented Jun 17, 2025

This PR optimizes the handling of empty Chain or Group workflows. Instead of scheduling a noop task and relying on the middleware to process completion callbacks, this change processes them directly within the Workflow.run method when there is no delay. This avoids an unnecessary round trip to the broker.

Why do we even support empty Chains and Groups?

Empty chains and groups are common when created programmatically:

Chain(
  Group(*[do_a_bunch_of_work.message(id) for id in work_items]),
  finalize_work.message(),
)

In this example, work_items might be empty, in which case finalize_work should run immediately.

@pencil pencil requested review from Copilot and dan-woz June 17, 2025 00:11
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Optimizes empty workflow handling by processing completion callbacks without scheduling an unnecessary no-op task when no delay is specified.

  • Adds tests for empty Chain and Group workflows.
  • Implements direct callback processing in Workflow.__schedule_noop when no delay is provided.
  • Introduces a new _process_completion_callbacks method in the middleware.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
dramatiq_workflow/tests/test_workflow.py Adds tests ensuring that empty workflows skip broker enqueue and process callbacks correctly.
dramatiq_workflow/_middleware.py Introduces _process_completion_callbacks to process workflow completion callbacks immediately.
dramatiq_workflow/_base.py Updates __schedule_noop to call _process_completion_callbacks when there's no delay, avoiding a no‑op task.

Copy link
Collaborator

@dan-woz dan-woz left a comment

Choose a reason for hiding this comment

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

coolio 😎

@pencil pencil merged commit fbadc74 into main Jun 23, 2025
9 checks passed
@pencil pencil deleted the optimize-noop branch June 23, 2025 17:59
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