3

Google's doc on async tasks assumes knowledge of the difference between regular and asynchronously added tasks.

add_async(task, transactional=False, rpc=None)
Asynchronously add a Task or a list of Tasks to this Queue.

How is adding tasks asynchronously different to adding them regularly.

I.e. what is the difference between using add(task, transactional=False) and add_async(task, transactional=False, rpc=None)

I've heard that adding tasks regularly blocks certain things. Any explanation of what it blocks and how, and how async tasks don't block would be greatly appreciated.

  • A normal task will block whatever thread it is running on. In a mobile context, UI events come on the "main" or "UI" thread. Having a UI event trigger some heavy code will block the main thread and the UI will freeze. This is a bad experience. Android has gone so far as to not allow networking from the UI thread. You now have to network asynchronously and the unwanted UI freeze will not be experienced – Jesse Black May 28 '14 at 21:56
1

The async version is simply not blocking the calling thread until the result of the task enqueuing RPC is available. This allows performing other operations before eventually blocking waiting for the RPC result, for example enqueueing multiple tasks in parallel, if needed, or performing ndb async calls.

It should be noted that you do need to supply an RPC object as the optional rpc= argument for the async call, it can't be None - it will be used later on to retrieve the result of the RPC.

From Adding tasks asynchronously:

By default, the Task Queue API calls are synchronous. For most scenarios, synchronous calls work fine. For instance, adding a task is usually a fast operation: the median time to add a task is 5 ms and 1 out of every 1000 tasks can take up to 300 ms. Periodic incidents, such as back-end upgrades, can cause spikes to 1 out of every 1000 tasks taking up to 1 second.

If you are building an application that needs low latency, the Task Queue API provides asynchronous calls that minimize latency.

Consider the case where you need to add 10 tasks to 10 different queues (thus you cannot batch them). In the worst case, calling queue.add() 10 times in a loop could block up to 10 seconds, although it's very rare. Using the asynchronous interface to add tasks to their respective queues in parallel, you can reduce the worst-case latency to 1 second.

If you want to make asynchronous calls to a task queue, use the asynchronous methods provided by the Queue class and an RPC object. Call get_result() on the returned RPC object to force the request to complete. When asynchronously adding tasks in a transaction, you should call get_result() on the RPC object before committing the transaction to ensure that the request has finished.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.