Forums

Slow API responses

Hi there, We host our web-app on 2 different pythonanywhere accounts. Our APIs respond in about 600ms on our dev. account but take about 4-5s on our prod. account.

We are stuck in finding ways to optimize this and cannot pinpoint whether it is related to our database, code, the server speed or something else.

Did anyone face the same and what did you do to solve the issue? Thanks!

That sounds very strange. Are they handling comparable amounts of data? If the prod system has significantly more data, and especially if your database needs indexes, then that could explain that kind of timing difference.

The best way to track things down is probably to add tracing prints in the views that are running slowly in production; for example, this:

print(f"{datetime.now()}: about to do XXXX", flush=True)

...will put a timestamped message into the website's server log, so by dotting those around you should at least be able to work out which parts of the code are running slowly, which might give some indication as to what needs to be changed or optimised.

Hi Gilles, Thanks!

We will implement the print statements to research it better. On the same topic: we also see different respons times during the day. One 1 moment it is fast, on the other very slow. Does this have to do with the package that we are using (account)?

Best regards!

There's no way we can tell from the name of a module whether it may be responsible for speed variance. Use the process that Giles outlined to get an idea of where your code is spending it's time and the source of the speed variance may become apparent.

Hi Glenn, Giles, We have been monitoring performance for a week and found that in fact the performance changes drastically during the days. During the days execution of queries on the database take considerable time, in our evenings (Dutch time) performance is fast.

My assumption is that the database servers are shared resources and therefore performance can be affected by other projects. Is this a valid assumption and if so, do you offer packages in which performance is not affected by shared resources?

Thanks!

Your assumptions are correct. We provide private servers for customers who don't want to be affected by shared resources. Please contact us at support@pythonanywhere.com to discuss details.

Ok, email has been sent!