本文共 11652 字,大约阅读时间需要 38 分钟。
python和golang
After a of Python, Ruby, and Golang for a command-line application I decided to use the same pattern to compare building a simple web service. I have selected (Python), (Ruby), and (Golang) for this comparison. Yes, there are many other options for web application libraries in each language but I felt these three lend well to comparison.
在对命令行应用程序的Python,Ruby和Golang进行之后,我决定使用相同的模式来比较构建简单的Web服务。 为了进行比较,我选择了 (Python), (Ruby)和 (Golang)。 是的,每种语言的Web应用程序库还有许多其他选择,但是我觉得这三个可以很好地进行比较。
This is a guest blog post by , a software engineer from Boulder.
这是一个客人博客文章由 ,从博尔德一名软件工程师。
Here is a high-level comparison of the libraries by .
这是对库的高层比较。
Flask is a micro-framework for Python based on Werkzeug, Jinja2 and good intentions.
Flask是基于Werkzeug,Jinja2和良好意图的Python微型框架。
For very simple applications, such as the one shown in this demo, Flask is a great choice. The basic Flask application is only 7 lines of code (LOC) in a single Python source file. The draw of Flask over other Python web libraries (such as or ) is that you can start small and build up to a more complex application as needed.
对于非常简单的应用程序,例如本演示中显示的应用程序,Flask是一个不错的选择。 基本的Flask应用程序在一个Python源文件中只有7行代码(LOC)。 Flask与其他Python Web库(例如或 )相比,其优势在于您可以从小规模开始,并根据需要构建更复杂的应用程序。
112233445566778899 |
Sinatra is a for quickly creating web applications in Ruby with minimal effort.
Sinatra是一种用于以最少的工作量在Ruby中快速创建Web应用程序。
Just like Flask, Sinatra is great for simple applications. The basic Sinatra application is only 4 LOC in a single Ruby source file. Sinatra is used instead of libraries such as for the same reason as Flask – you can start small and expand the application as needed.
就像Flask一样,Sinatra非常适合简单的应用程序。 基本的Sinatra应用程序在单个Ruby源文件中只有4个LOC。 出于与Flask相同的原因,使用Sinatra代替了诸如之类的库–您可以从小处着手,并根据需要扩展应用程序。
1122334455 |
Martini is a powerful package for quickly writing modular web applications/services in Golang.
Martini是一个功能强大的软件包,用于在Golang中快速编写模块化的Web应用程序/服务。
Martini comes with a few more batteries included than both Sinatra and Flask but is still very lightweight to start with – only 9 LOC for the basic application. Martini has come under some by the Golang community but still has one of the highest rated Github projects of any Golang web framework. The author of Martini responded directly to the criticism . Some other frameworks include , , and even the built-in library.
马提尼酒比西纳特拉酒瓶和Flask酒瓶附带的电池要多一些,但一开始仍然非常轻巧-基本应用只有9 LOC。 Martini受到Golang社区的 ,但仍然是所有Golang Web框架中评分最高的Github项目之一。 马提尼酒的作者在直接回应了批评。 其他一些框架包括 , 甚至内置的库。
11223344556677889910101111 |
With the basics out of the way, let’s build an app!
借助基础知识,让我们构建一个应用程序!
The service created provides a very basic blog application. The following routes are constructed:
创建的服务提供了一个非常基本的博客应用程序。 构建了以下路线:
GET /
: Return the blog (using a template to render).GET /json
: Return the blog content in JSON format.POST /new
: Add a new post (title, summary, content) to the blog.GET /
:返回博客(使用模板进行渲染)。 GET /json
:以JSON格式返回博客内容。 POST /new
:向博客添加新帖子(标题,摘要,内容)。 The external interface to the blog service is exactly the same for each language. For simplicity MongoDB will be used as the data store for this example as it is the simplest to set up and we don’t need to worry about schemas at all. In a normal “blog-like” application a relational database would likely be necessary.
每种语言的博客服务外部接口完全相同。 为简单起见,MongoDB将用作此示例的数据存储,因为它是最简单的设置,我们完全不需要担心架构。 在正常的“类博客”应用程序中,可能需要关系数据库。
POST /new
POST /new
11223344 |
GET /
GET /
GET /json
GET /json
1122334455667788991010 |
Each application can be broken down into the following components:
每个应用程序可以细分为以下组件:
GET /json
)GET /
)GET /json
) GET /
) The rest of this article will compare each of these components for each library. The purpose is not to suggest that one of these libraries is better than the other – it is to provide a specific comparison between the three tools:
本文的其余部分将比较每个库的每个组件。 目的并不是要建议这些库中的一个优于另一个,而是要提供这三个工具之间的特定比较:
All of the are bootstrapped using and . Before diving into how each application is bootstrapped under the hood we can just use docker to get each up and running in exactly the same way – docker-compose up
使用和引导所有 。 在深入探讨如何在后台启动每个应用程序之前,我们可以使用docker以完全相同的方式启动和运行每个应用程序– docker-compose up
Seriously, that’s it! Now for each application there is a Dockerfile
and a docker-compose.yml
file that specify what happens when you run the above command.
说真的,就是这样! 现在,对于每个应用程序,都有一个Dockerfile
和Dockerfile
docker-compose.yml
文件,它们指定运行上述命令时发生的情况。
112233445566 |
This Dockerfile
says that we are starting from a base image with Python 3.4 installed, adding our application to the /app
directory and using to install our application requirements specified in requirements.txt
.
该Dockerfile
表示,我们从安装了Python 3.4的基础映像开始,将我们的应用程序添加到/app
目录中,并使用来安装requirements.txt中指定的应用程序requirements.txt
。
112233445566 |
This Dockerfile
says that we are starting from a base image with Ruby 2.2 installed, adding our application to the /app
directory and using to install our application requirements specified in the Gemfile
.
这个Dockerfile
表示,我们从安装了Ruby 2.2的基本映像开始,将我们的应用程序添加到/app
目录,并使用安装我们在Gemfile
指定的应用程序需求。
112233445566778899 |
This Dockerfile
says that we are starting from a base image with Golang 1.3 installed, adding our application to the /go/src/github.com/kpurdon/go-blog
directory and getting all of our necessary dependencies using the go get
command.
这个Dockerfile
表示我们从安装Golang 1.3的基础映像开始,将我们的应用程序添加到/go/src/github.com/kpurdon/go-blog
目录中,并使用go get
命令获取所有必要的依赖项。
11223344556677 |
11 |
1122 |
11 |
11223344556677889910101111121213131414 |
11 |
112233445566778899 |
112233445566778899 |
11223344556677889910101111121213131414151516161717 |
Flask provides a method but since the service is using MongoDB the mongodb bson utility is used.
Flask提供了方法,但是由于该服务使用的是MongoDB,因此使用了mongodb bson实用程序。
1122 |
112233 |
11 |
11 |
11223344556677889910101111121213131414 |
11 |
11223344556677889910101111121213131414 |
11 |
11223344556677889910101111121213131414 |
All of the applications are using the mongodb driver specific to the language. The environment variable DB_PORT_27017_TCP_ADDR
is the IP of a linked docker container (the database ip).
所有应用程序都使用特定于该语言的mongodb驱动程序。 环境变量DB_PORT_27017_TCP_ADDR
是链接的DB_PORT_27017_TCP_ADDR
容器的IP(数据库ip)。
112233 |
112233 |
11223344 |
11223344556677 |
11 |
11 |
11 |
11 |
1122 |
A great solution to deploying all of these applications is to use and .
部署所有这些应用程序的绝佳解决方案是使用和 。
Dockerfile
Docker文件
112233445566 |
docker-compose.yml
docker-compose.yml
112233445566778899101011111212 |
Dockerfile
Docker文件
112233445566 |
docker-compose.yml
docker-compose.yml
112233445566778899101011111212 |
Dockerfile
Docker文件
112233445566 |
docker-compose.yml
docker-compose.yml
112233445566778899101011111212 |
To conclude lets take a look at what I believe are a few categories where the presented libraries separate themselves from each other.
最后,让我们看一下我认为所提供的库彼此分开的几个类别。
While Flask is very lightweight and reads clearly, the Sinatra app is the simplest of the three at 23 LOC (compared to 46 for Flask and 42 for Martini). For these reasons Sinatra is the winner in this category. It should be noted however that Sinatra’s simplicity is due to more default “magic” – e.g., implicit work that happens behind the scenes. For new users this can often lead to confusion.
虽然Flask非常轻巧,而且读起来很清晰,但Sinatra应用是这三个应用中最简单的,它的LOC为23(Flask为46,马丁尼为42)。 由于这些原因,Sinatra是该类别的赢家。 但是,应该注意的是Sinatra的简单性是由于更多默认的“魔术”所致–例如,幕后发生的隐性工作。 对于新用户,这通常会导致混乱。
Here is a specific example of “magic” in Sinatra:
这是Sinatra中“魔术”的一个具体示例:
11 |
And the equivalent Flask code:
和等效的Flask代码:
112233445566 |
For beginners to programming Flask and Sinatra are certainly simpler, but for an experienced programmer with time spent in other statically typed languages Martini does provide a fairly simplistic interface.
对于初学者来说,Flask和Sinatra肯定更简单,但是对于有经验的程序员,他们花时间在其他静态类型的语言上,Martini确实提供了一个相当简单的界面。
The Flask documentation was the simplest to search and most approachable. While Sinatra and Martini are both well documented, the documentation itself was not as approachable. For this reason Flask is the winner in this category.
Flask文档是最容易搜索且最易于访问的文档。 尽管Sinatra和Martini都有很好的文档,但是文档本身并不那么容易获得。 因此,Flask是该类别的赢家。
Flask is the winner hands down in this category. The Ruby community is more often than not dogmatic about Rails being the only good choice if you need anything more than a basic service (even though offers this on top of Sinatra). The Golang community is still nowhere near a consensus on one (or even a few) web frameworks, which is to be expected as the language itself is so young. Python however has embraced a number of approaches to web development including Django for out-of-the-box full-featured web applications and Flask, Bottle, CheryPy, and Tornado for a micro-framework approach.
Flask是此类别中的获胜者。 如果您除了基本服务之外还需要其他任何东西(即使在Sinatra之上提供了此功能),Ruby社区通常会坚信Rails是唯一的好选择。 Golang社区在一个(或什至几个)Web框架上仍未达成共识,这是因为它本身还很年轻。 但是,Python接受了许多Web开发方法,其中包括Django用于开箱即用的全功能Web应用程序,以及Flask,Bottle,CheryPy和Tornado用于微框架方法。
Note that the point of this article was not to promote a single tool, rather to provide an unbiased comparison of Flask, Sinatra, and Martini. With that said, I would select Flask (Python) or Sinatra (Ruby). If you are coming from a language like C or Java perhaps the statically-typed nature of Golang may appeal to you. If you are a beginner, Flask might be the best choice as it is very easy to get up and running and there is very little default “magic”. My recommendation is that you be flexible in your decisions when selecting a library for your project.
请注意,本文的目的不是要推广单个工具,而是要对Flask,Sinatra和Martini进行公正的比较。 话虽如此,我会选择Flask(Python)或Sinatra(Ruby)。 如果您来自C或Java之类的语言,那么Golang的静态类型本质可能会吸引您。 如果您是初学者,则Flask可能是最好的选择,因为它很容易启动和运行,并且几乎没有默认的“魔术”。 我的建议是,在为项目选择库时,您可以灵活地做出决定。
翻译自:
python和golang
转载地址:http://nwhwd.baihongyu.com/