One, foreword

Python has exploded in popularity in recent years. It is widely used in network development operations, data science, network development, and network security issues.

However, Python has no advantage at all in terms of speed.

How does Java compare to C, C++, C#, or Python in terms of speed? The answer depends almost entirely on the application you want to run. There is no perfect benchmark on this subject, but The Computer Language Benchmarks Game is a good one.

I wanted to answer the question: why is Python 2 to 10 times slower than other languages when running the same program? Why can’t we make it faster?

Here are the main reasons:

  • “It is the GIL (Global Interpreter Lock)”
  • “It’s an interpreted language, not a compiled language.”
  • “It’s a dynamically typed language”

Today, instead of focusing on which of these causes has the biggest impact on performance, we’ll talk about what we can do to make Python3.7 faster than expected, much faster

Two, start exploring

Python introduced the syntax sugars async and await for coroutines in version 3.5. This means we can write Python applications as coroutines, which will make Python code run asynchronously, just like Node

Of course, that’s not our goal today

Three, get down to business

Among many Web frameworks, Django, Tornado and Flask are relatively mature, and Tornado and others can also use asynchronous options, but we won’t cover them today

We are going to talk about Sanic who is tough at the moment

I think the main reason why I’m talking about Sanic is that he’s fast enough and he can build things quickly, so he’s like Flask, you know

Here are the official benchmark results

Sanic’s developers say they were inspired by this article, UvLoop: Blazing Fast Python Networking

This is how I felt after using it for two years

  • Fast enough to take off

  • Fast build speed

  • Native support for async/await syntax

  • The community is coming alive, and there are some great plugins coming out compared to the immature ones

  • Automatic allocation & management processes

Just a quick comment, the purpose of today is not to teach people how to use Sanic, but to hope that more people join Sanic and experience the convenience of Sanic and the emergence of more excellent frameworks

Attached is the official Sanic document

Third, the advanced

Having said that, one more problem is that my Web uses coroutines, which means that using normal Pymysql and Psycopg2 would block our threads while working with the database, and our Web service would be stuck in the database no matter how fast it is. Here are some of the coroutine support libraries I often use

  • Postgresql: asyncpg
  • Redis: aioredis
  • The I/O Framework: uvloop

These are some libraries I usually use, I like PG, as for mysql and other databases, you can search by yourself

Since Sanic came out, I have used it to build Web applications large and small, including its increasingly sophisticated community and various aspects of support, such as the emergence of libraries such as Sanic_session, to make it less difficult and powerless in large production environments. So far I have been keen to explore his widget, if you have better Sanic support, please leave a comment

Wipe your ass

I will spare some time to update the Sanic and family bucket tutorial below, if lazy, lazy, lazy to read the official document, just wait for me to update it!

  • Sanic: Python makes your Web applications fly like Sanic
  • asyncpg
  • aioredis
  • uvloop
  • sanic_session
  • sso
  • crypto/decrypt
  • golang mixin
  • middleware
  • pub/sub/listeners