Gzipping all HTTP traffic with Pyramid

后端 未结 3 444
别跟我提以往
别跟我提以往 2021-01-02 01:03

I am creating a mobile service based on Pyramid framework. Because it\'s mobile everything to reduce bandwidth usage is plus. I am considering gzipping all the traffic, even

3条回答
  •  暖寄归人
    2021-01-02 01:24

    First of all I should stress that you should do this on the web server level (nginx or apache). There are several reasons for this:

    1. Performance - If you do this in Python you are using one of your threads that could be handling requests to do cpu-intensive compression. This is way less efficient than allowing your optimized web server to handle it.

    2. Blocking - Most GZip middleware will block your responses, buffering the body so that it can compress the entire response. This is a huge problem if you are attempting to stream any response back to the client because it will get caught in middleware. This is actually a violation of PEP333, the WSGI specification.

    With all of this in mind, it might make sense to do it in Python at least for debugging purposes during development.

    Since you're already using Pyramid then you have Paste installed. Thus you can simply add the paste.gzipper.GzipMiddleware to your application's pipeline like so:

    [filter:gzip]
    use = egg:Paste#gzip
    compress_level = 6
    
    [pipeline:main]
    pipeline =
        gzip
        app
    

    Obviously if you don't want to change the compression level from the default of 6 you can simply add the egg:Paste#gzip to the pipeline in place of configuring the filter and giving it a custom name (gzip).

提交回复
热议问题