5 ms·
> Go back and look. I said that it forks and then it imports your app. Your app (which is almost certainly the bulk of the code inside the Python interpreter) i
by orf 7y ago
> Go back and look. I said that it forks and then it imports your app. Your app (which is almost certainly the bulk of the code inside the Python interpreter) is not actually in memory when the fork happens. It gets loaded AFTER that point.
You can just pass `--preload` to have gunicorn load the application once. If you're using a standard framework like Django or Flask and not doing anything obviously insane then this works really well and without much effort. Yeah I'm sure some dumb libraries do some dumb things, but that's on them, and you for using those libraries. Same as any language.
If you want to stick your nose up at Python and state outright "I will not write a service in it" then that's up to you, it just comes across as your loss rather than a damning condemnation of the language and it's ecosystem from Rachel By The Bay, an all-knowing and experienced higher power. I guess everyone else will keep quickly shipping value to customers with it while you worry about five processes waking up from a system call at once or an extra 150mb of memory usage.
- ary 7y ago> You can just pass `--preload` to have gunicorn load the application once. If you're using a standard framework like Django or Flask and not doing anything obviously insane then this works really well and without much effort. Yeah I'm sure some dumb libraries do some dumb things, but that's on them, and you for using those libraries. It's not always trivial to ensure none of your dependencies have import-time side effects. Sometimes the productivity/business benefit provided by the depedendency outweighs the pain introduced by the side effects.
- deleted 7y ago[deleted]
- Skunkleton 7y agoIf spinning up a few more workers will solve a performance problem for you, it’s probably worth the time to throw the preload flag on there and see what it does to your test suite. Since you are already cost optimizing at this point you probably have the time.
- kodablah 7y ago> everyone else will keep quickly shipping things with it while you worry about five processes waking up from a system call at once or an extra 150mb of memory usage With the current state of ecosystems, this quality-vs-quantity mutual exclusivity is much less pronounced. These days, you can fire up these services as quick or quicker than in Python with better performance and resource usage that is also more maintainable. Unless you speak of highly ecosystem-dependant libraries (e.g. ML), Python defenses that rely on time to market say more about the author's narrow comfort than general expediency.
- orf 7y agoCitation needed. You can write anything you want in any language you want, but if your team is experienced with Python then they will continue to ship value quickly. Sure, maybe if they were all well versed in brainfuck they could ship things quicker. Narrowing in on the rather specific point about shipping things with Python and ignoring the larger argument that it doesn't freaking matter if things are not as efficient as they could possibly be is quite odd to be honest. I'm sure some of the arguments in the blog post would apply to whatever language you had in mind while writing your reply.
- nimish 7y agoOf course weird domain specific libraries are a major reason to use python. I can get my oddball service up in a few days vs weeks or months to replicate some bizarre library. If all you're doing is basic web stuff then sure you can do it in Lang du jour but even after a decade go doesn't have a competent xml library for example. Time to market is killer. Python buys you time to determine whether your product is even worth building. Deployment sucks though. e: I literally could not have written the services in my current role in a language other than Java or Python without replicating 100kloc libraries. Java would have required a bunch of work to integrate with the other services we had. So: python. If that costs me an extra $1k/mo for servers but gets us a customer paying $100k a month, was it wasteful?
- lmm 7y agoWhy was it easier to integrate with those other services from Python than from Java? Only because you already picked Python for those, right? (I agree with you about time to market being the important thing. I don't think Python is winning that game any more though: its dependency management and deployment has fallen behind the rest of the industry, and newer languages have largely caught up with its conciseness without having to make the same compromises)
- closeparen 7y agoThe whole discipline Rachel writes about is clearly intended for mature, scaled operations where outages and inefficiencies are legitimately worth much more than the systems wizards to stop them. There’s a time and a place for “move fast and break things” and if that’s where you are, it’s probably not for you.
- worik 7y ago"The whole discipline Rachel writes about is clearly intended for mature, scaled operations where outages..." That is not true. RotB is describing saftware inefficiencies that we learnt to do without 20 years plus ago. Because the Python hackers that built all these tools did not pay attention, when they built new tools they recreated the old problems. Worik's 23.6918th rule of creativity: It is easier to write than read.
- FridgeSeal 7y agoI don’t think you guys are saying different thing here. The article in this case is describing a bunch of common processes/optimisations/features that we have learnt to be critical for effective and efficient running of software. The author does this because the audience she writes for is, as the previous comment puts it “mature, scaled operations where outages...” etc etc
- cakoose 7y agoEven if you manage to preload everything you need, Python's reference counting mechanism will cause everything to be copied anyway. Every time you access an object, its reference count is mutated, which will the memory page to be copied. There are workarounds if you're willing to mess with the Python interpreter: https://instagram-engineering.com/copy-on-write-friendly-python-garbage-collection-ad6ed5233ddf https://instagram-engineering.com/copy-on-write-friendly-pyt...
- lozenge 7y agogc.freeze() reached Python 3.7 as mentioned there, I'm not sure how many web frameworks are using it though.
- ghostwriter 7y ago> Python's reference counting mechanism will cause everything to be copied anyway. isn't it CPython specific though, and may not be observed when running on PyPy?
- dfox 7y agoIt is. And in practice it is far from everything, only stuff that is actively directly referenced by the service code.
- pas 7y agoIt still helps with the loaded .so files and whatnot. The code objects and other things that are immutable. (CPython refcounts those too?)
- pdonis 7y ago> The code objects and other things that are immutable. (CPython refcounts those too?) CPython refcounts all objects. Refcounting is not required because of mutability; it's required because the interpreter needs to know when an object's memory can be reclaimed for something else. I don't know if code objects specifically would have their refcounts mutated a lot, since typically they're only referenced by one object, the function that they're the code for. But function objects will have their refcounts mutated every time the function is called, since that sets up a stack frame that grabs a reference to the function object and then releases it when the function returns.
- rossmohax 7y ago> If you're using a standard framework like Django or Flask then this works really well and without much effort. I dug into it ~year ago, Django loads almost everything lazily, so simple --preload did next to nothing. I had to write code to load app for real at import time, exact thing article and common wisdom tells us not to do.
- orf 7y agoDjango loads some things lazily but not almost everything. Nearly all your imports should be loaded by the preloaded application and shared across forks (COW semantics aside), and this usually takes up a non-trivial amount of memory. The things that are lazy are usually lazy for a reason - database connections, caching etc. I believe the i18n system is also lazily loaded and depending on the languages you configure it can take up a fair bit of memory.
- sneak 7y ago> a damning condemnation of the language and it's ecosystem from Rachel By The Bay, an all-knowing and experienced higher power If I were her, I’d put that on my CV. :D