I have a Python function that adds a task queue for each email address in my mailing list (thousands each time I mail them). The problem is that, even though each mail was to be sent via execution in a task queue, I still get this dreaded error:
DeadlineExceededError: The API call mail.Send() took too long to respond and was cancelled.
Any solutions?
The deadline happens during communication between your GAE instance and the RPC server that handles the
mail.Sendcall. This, in turn, might indicate internal problems of GAE or (more likely) failure to communicate with SMTP server in timely manner.The latter is conceptually very similar to deadline on URLFetch call. It is possible, however, to set a custom deadline for URLFetch which largely alleviates that problem.
Unfortunately, there is no documented analogy for Mail API. There is workaround, though, that involves providing your own
make_sync_callmethod – which allow for more lenient deadline – as parameter ofEmailMessage.send(). To produce such a method, you need to delve into the internals of Python’s interface used to make GAE RPC calls. The solution I find working looks as follows:You can then use it to supply your custom deadline which will be honored by the resulting
make_sync_callmethod:If you want to know more about what happens under the curtains of GAE RPC calls, I suggest reading Nick Johnson’s blog post about it. This is good starting point if you’d ever want to go through the Python’s GAE RPC bindings in order to solve similar issues.