Jeremy
Jeremy

Reputation: 671

Flask Celery: Event synchronization type

I have pretty unique behavior I need to achieve with celery. I understand that it is not recommended to have tasks block at all, however I think it is necessary here as I describe below. Pseudocode:

Task 1:
Set event to false
Start group of task 2
Scrape website every few seconds to check for changes
If changes found, set event

Task 2:
Log into website with selenium.
Block until event from Task 1 is set
Perform website action with selenium

I would want task2 to be executed multiple times in parallel for multiple users. Therefore checking the website for updates in each instance of task2 would result in a large number of requests to the website which is not acceptable.

For a normal flow like this, I would to use task1 to start login tasks in a group and start another group after the condition has been met to execute the action tasks. However, the web action is time-sensitive and I don't want to re-open a new selenium instance (which would defeat the purpose of having this structure in the first place).

I've seen examples like this: Flask Celery task locking but using a Redis cache seems unnecessary for this application (and it does not need to be atomic because the 'lock' is only modified by task1). I've also looked into Celery's remote control but I'm not sure if there is the capability to block until a signal is received.

There is a similar question here which was solved by splitting the task I want to block into 2 separate tasks, but again I can't do this.

Upvotes: 0

Views: 195

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24956

Celery tasks can themselves enqueue tasks, so it's possible to wait for an event like "it's 9am", and then spawn off a bunch of parallel tasks. If you need to launch an additional task on the completion of a group of parallel tasks (i.e., if you need a fan-in task at the completion of all fan-out tasks), the mechanism you want is chords.

Upvotes: 1

Related Questions