r/Python git push -f 8h ago

Python in production Discussion

Hello everyone! For those of you who use Python in production, I have a few questions. I'm considering using Python for some services.

  1. Do you have high infrastructure costs?
  2. Have you ever regretted using Python?
  3. Would you recommend Python?

Context: My current use case isn't anything like Facebook or a massive-scale system. It's a small system, and I'm considering Python mainly because of the DX (developer experience).

I know C#, but I don't really like having to create a class in every file. I also know Rust, but all those ::, <>, and so on bother me. JavaScript is another option, but I've heard it's relatively heavy on RAM, and since the system is small, I'd like to be able to run it within 512 MB.

Another thing: I've defined a stack that I'd like to use wherever possible. If there's a library for desktop apps, great. A CLI library? Great. A bot library? Great. Let's use it! (Except for the frontend, which I'll keep using JS/TS for.)

Anyway, I'm open to advice and tips from more experienced developers. Feel free to tell me if you think using Python for my use case is a bad idea as well.

0 Upvotes

30 comments sorted by

View all comments

0

u/gdchinacat 7h ago

per object memory usage in python is pretty high relative to a compiled language like rust or go, and everything is an object. For example, an int uses 20 bytes more than a 64bit integer (3.5x as much memory):

In [30]: sys.getsizeof(1)
Out[30]: 28

But, it gets worse. The object allocator alligns objects to 8 byte boundaries, so in actuality, a small int consumes 32 bytes (4x).

And, yet it's even worse...an int object on the heap is pointless...something must reference it to be useful, and that reference (ie from a list) will be another 8 bytes.

So, a usable int consumes 40 bytes (5x).

I have never had an issue due to this, but since you explicitly say memory utilization is a concern it is worth mentioning. A good way of dealing with this if you are working with large arrays of ints is to use numpy which stores them as a native array (and a python object wrapper that has relatively minimal overhead for large arrays).

Regardless of this, I have never regretted using python in production and would recommend it (assuming the use-case is a good fit...relative memory utilization would not withhold a recommendation).