How to wait for frappe.enqueue?

Hi,
1) I use frappe.enqueue() to run a method. And after this enqueue finish I want to run another method (not enqueued). But since the enqueue takes some times, the second method runs before the enqueued method finish.

enqueue('first_method')
second_method()

How to wait for the first enqueue to finish and then run the second method?

2) On other case, if I have a set of enqueues that runs together (e.g because 2 users click the same button at almost the same time):

def button_click():
    enqueue(third_method)    #while this method runs for user1, user2 clicks the button so it's also enqueued for user2
    enqueue(fourth_method)

So to illustrate the background jobs will be like this:

third_method(user1)
third_method(user2)
fourth_method(user1)
fourth_method(user2)

how do I make the fourth_method(user1) to run right after the third_method(user1) finish, without waiting for the third_method(user2) to finish?

I hope my description and questions are clear. Thank you

frappe.enqueue() is for asynchronous execution.

In the first case if you have to wait for the first method to finish executing, then why not simply do

first_method()
second_method()

Also you can do

enqueue('first_method', now=True)
second_method()

Both have the same effect.

In the second case. why not do?

def button_click():
    third_method()
    fourth_method()

Or if you want button_click() to return early

def button_click():
    enqueue("async_button_click")

def async_button_click():
    third_method()
    fourth_method()

If you don’t want asynchronous execution, don’t use the method that is built for asynchronous execution

1 Like

I’ve lost you. Can’t you show real snippets, instead of making up toy examples?

Sorry, I made wrong comment :pray:
But I understand your suggestion and will try the same.