hello world运行_在不到5分钟的时间内运行快速的Hello World服务器

hello world运行

This article is an informal introduction to Swift. It focuses on what makes Swift appealing and show how to easily start using it server-side. Future articles will cover more in depth Swift use cases as well as its syntax.

本文是对Swift的非正式介绍。 它着重于使Swift具有吸引力的方面,并展示了如何轻松地在服务器端开始使用它。 未来的文章将更深入地介绍Swift用例及其语法。

斯威夫特的简短故事 (A brief story of Swift)

Swift was created by Apple in 2014 setting it to become the dominant language for iOS development. Being backed up by one of the most influential tech companies in the world is quite a thing. In addition, farsightedly, Apple open-sourced Swift in 2015. Not surprisingly, it evolved to encompass uses ranging from systems programming to mobile and desktop apps, scaling up to cloud services as well as Machine Learning.

Swift由苹果公司于2014年创建,使其成为iOS开发的主导语言。 得到世界上最有影响力的科技公司之一的支持是一件很重要的事情。 此外,有远见的是,Apple在2015年开源了Swift。毫无疑问,它演变为涵盖了从系统编程到移动和桌面应用程序,范围扩大到云服务以及机器学习的各种用途。

语言敏捷 (Swift the language)

Swift is a general-purpose, multi-paradigm, compiled and high-performance system programming language. Swift philosophy gravitates around the following principles:

Swift是一种通用的,多范式的,已编译的高性能系统编程语言。 斯威夫特哲学遵循以下原则:

  • Safe. Its syntax encourages you to write clean and consistent code and is safe by default. Though, you can always opt for the unsafe option to make parts of your code run faster.

    安全。 它的语法鼓励您编写简洁一致的代码,并且默认情况下是安全的。 但是,您始终可以选择不安全的选项,以使部分代码更快地运行。

  • Fast. Swift is intended as a replacement for C-based languages (C, C++, and Objective-C). Not surprisingly, it is being adopted for Deep Learning and Differential Computing.

    快速。 Swift旨在替代基于C的语言(C,C ++和Objective-C)。 毫不奇怪,它已被深度学习和差分计算采用 。

  • Expressive. The official Swift documentation states that “(Swift) syntax that is a joy to use”, I can only agree with that. It’s truly a pleasure to code with such elegant syntax.

    富有表现力。 Swift的官方文档指出“(使用Swift语法)很有趣”,我只能同意这一点。 使用如此优雅的语法进行编码确实是一种乐趣。

Enough with the Swift propaganda, let’s talk about why I got hung up on Swift in the first place.

借助Swift的宣传已经足够了,让我们谈谈为什么我一开始就对Swift感兴趣。

Swift和AI (Swift and AI)

I came across Swift while following the Fastai Deep Learning course. I was astonished to find that the two of the fourteen lectures are about Swift: that’s about 15% of the content ! In fact, a new Fastai implementation with Swift might soon see the light.

在学习Fastai深度学习课程时,我遇到了Swift。 我惊讶地发现十四讲中的两篇都是关于Swift的:占内容的15%! 实际上,使用Swift进行的Fastai新实现可能很快就会见效。

Google is also in the game with Swift for TensorFlow.

谷歌也在TensorFlow的 Swift中参与其中。

Swift for TensorFlow is a next-generation platform for machine learning, incorporating the latest research across machine learning, compilers, differentiable programming, systems design, and beyond. This is an early-stage project: it is not feature-complete nor production-ready, but it is ready for pioneers to try in projects, give feedback, and help shape the future!

Swift for TensorFlow是用于机器学习的下一代平台,它结合了有关机器学习,编译器,差异化编程,系统设计以及其他方面的最新研究成果。 这是一个早期阶段的项目:它既不具备功能完备性,也没有生产准备就绪,但它为开拓者准备尝试项目,提供反馈并帮助塑造未来!

Why suddenly Swift, a seemingly iOS/macOS orientated language is getting so much traction in machine learning?

为什么突然间,一种看似iOS / macOS导向语言的Swift在机器学习中获得了如此大的吸引力?

Deep learning is computationally expensive, passing huge data sets through long chains of tensor operations can be quite challenging. In fact, Python has a limit in terms of how far it can perform these calculations quickly.

深度学习在计算上很昂贵,通过长条张量操作链传递大量数据集可能会非常具有挑战性。 实际上, Python在快速执行这些计算方面有一个限制。

Any deep learning practitioner using a language other than Python is giving up a vast ecosystem of interconnected libraries. The problem is that Python is neither designed to be fast nor to be safe. Instead, it is designed to be easy and flexible. As a matter of fact, performance problems have been reduced by using libraries written in other languages such as C and C++ (Numpy, Pytorch, Tensorflow…). To make Python safer, a mechanism of optional type annotations has been introduced. These are not powerful enough though and only mitigate the problem. The C and C++ libraries introduce also another layer of complexity: any deep learning practitioner will have a hard time debugging Python code that uses these libraries.

使用Python以外的语言的任何深度学习从业人员都将放弃庞大的互连库生态系统。 问题在于,Python既不设计为快速也不安全。 相反,它的设计既简单又灵活。 实际上,通过使用以其他语言(例如C和C ++) (Numpy,Pytorch,Tensorflow等)编写的库可以减少性能问题。 为了使Python更安全,引入了可选类型注释的机制。 但是这些功能不够强大,只能缓解问题。 C和C ++库还引入了另一层复杂性:任何深度学习从业人员都将很难调试使用这些库的Python代码。

Swift is designed to be almost as fast as C and C++, safe and expressive. It clearly has all the ingredients to reach the next stage of adoption.In particular, we are already able to really easily run Swift server-side. Let’s see how.

Swift被设计为几乎与C和C ++一样快,安全且富有表现力。 它显然具有进入下一阶段采用的所有要素。尤其是,我们已经能够真正轻松地运行Swift服务器端。 让我们看看如何。

服务器端Swift (Server-side Swift)

If we want to be able to deploy ML models at scale in the near future, we must start by deploying servers with Swift.

如果我们希望能够在不久的将来大规模部署ML模型,则必须从使用Swift部署服务器开始。

When it comes to server-side Swift, there are a few solutions but not too many yet. The two I took into considerations are Vapor and Kitura. The former started as an independent open-source project whereas the latter started as an internal project at IBM which controlled it and sponsored it until its inception. Vapor syntax is closer to Swift language and to functional programming style. So I decided to use it. You can read more about the difference between the two here.

对于服务器端Swift,有一些解决方案,但还没有太多。 我考虑的两个是Vapor和Kitura 。 前者开始于一个独立的开源项目,而后者开始于IBM的一个内部项目,该项目由IBM控制并赞助,直到成立。 蒸气语法更接近Swift语言和函数式编程风格。 所以我决定使用它。 您可以在此处详细了解两者之间的区别。

To be able to deploy Swift services at ease, the best solution is to use Docker. For those unfamiliars, Docker can run software packages in self-contained isolated environments called containers. These containers bundle their own tools, libraries, and configuration files. Containers can communicate with each other through well-defined channels.

为了能够轻松部署Swift服务,最好的解决方案是使用Docker 。 对于那些不熟悉的人,Docker可以在称为容器的自包含隔离环境中运行软件包。 这些容器捆绑了它们自己的工具,库和配置文件。 容器可以通过定义明确的渠道相互通信。

If you don’t want to spend your days setting up and configuring your work environment then you might think about trying Docker out.

如果您不想花费很多时间来设置和配置工作环境,则可以考虑尝试使用Docker。

In what follows, I assume you have already installed Docker CLI. If you don’t, please visit this link and install it .

在接下来的内容中,我假设您已经安装了Docker CLI。 如果没有,请访问此链接并安装。

Without further ado, let’s jump on the code!

事不宜迟,让我们跳上代码!

安装Vapor并创建一个新项目 (Installing Vapor & creating a new project)

First of all, let’s install vapor toolbox:

首先,让我们安装蒸气工具箱:

git clone https://github.com/vapor/toolbox.git
cd toolbox
# git checkout 
swift build -c release --disable-sandbox
mv .build/release/vapor /usr/local/bin

Type the following to check everything works as expected.

键入以下内容以检查一切是否正常。

vapor --help

You should see a list of available commands.

您应该看到可用命令的列表。

hello world运行_在不到5分钟的时间内运行快速的Hello World服务器_第1张图片

We can now use vapor CLI commands to create a new project. Notice that I left a change directory command commented as you might want to create the project in a new directory if you still are in the toolbox directory.

现在,我们可以使用vapor CLI命令创建一个新项目。 请注意,我留下了一条注释更改目录命令,因为如果您仍在toolbox目录中,则可能要在新目录中创建项目。

# cd ..
vapor new helloWorld

We won’t be covering the app organization in this article. As a line guide, think of the main.swift as the entry-point of your app where you bootstrap the server and do that stuff your server needs to start working. The sources folder contains your application whereas Tests, remarkably, contains the tests. Package.swift is the manifest file for your project.

本文将不涉及应用程序组织。 作为线路指南,可以将main.swift视为应用程序的入口点,您可以在其中引导服务器并执行服务器开始工作所需的工作。 sources文件夹包含您的应用程序,而Tests,显着包含测试。 Package.swift是您的项目的清单文件。

Instead, we are quickly going to look into the routes file. So, let’s just navigate to the swift file containing the routes and replace the string “world” with an emoji.

相反,我们将快速查看routes文件。 因此,让我们导航到包含路线的快捷文件,然后用表情符号替换字符串“ world”。

Open the newly created project with your favorite editor. On Visual Studio Code, it should look like the following:

用您喜欢的编辑器打开新创建的项目。 在Visual Studio Code上 ,它应如下所示:

hello world运行_在不到5分钟的时间内运行快速的Hello World服务器_第2张图片
Vapor starter codebase 蒸气启动器代码库

As you can see the syntax is sweet and clean. First, you import Vapor and you define a throwable function called route. Then, inside that function, you define two get functions. In our example, we define two GET endpoints: / and /hello.

如您所见,该语法既简洁又干净。 首先, import Vapor并定义一个称为route的可抛出函数。 然后,在该函数内部,定义两个get函数。 在我们的示例中,我们定义了两个GET端点: //hello

Next, let change what Go inside the function app.get(“hello”) and replace the return statement like so:

接下来,让我们在函数app.get(“hello”)更改Go并替换return语句,如下所示:

app.get("hello") { req -> String in
//     return "Hello, World!"
    return "Hello, !"
}

使用Docker构建并运行服务器。 (Build and run the server with Docker.)

Vapor ‘new’ command automatically generates a Dockerfile inside the project directory. It should look like the following.

Vapor的“ new”命令会在项目目录中自动生成一个Dockerfile。 它应如下所示。

# ================================
# Build image
# ================================
FROM vapor/swift:latest as build
WORKDIR /build


# First just resolve dependencies. This creates a cached layer that can be reused 
# as long as your Package.swift/Package.resolved files do not change.
COPY ./Package.* ./
RUN swift package resolve


# Copy entire repo into container
COPY . .


# Compile with optimizations
RUN swift build \
	--enable-test-discovery \
	-c release \
	-Xswiftc -g


# ================================
# Run image
# ================================
FROM vapor/ubuntu:18.04
WORKDIR /run


# Copy build artifacts
COPY --from=build /build/.build/release /run
# Copy Swift runtime libraries
COPY --from=build /usr/lib/swift/ /usr/lib/swift/
# Uncomment the next line if you need to load resources from the `Public` directory
#COPY --from=build /build/Public /run/Public


ENTRYPOINT ["./Run"]
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0"]

Since you don’t need the Swift compiler to run your server, the Dockerfile separates build and run images. Consequently, the final output of the Dockerfile will be extremely lightweight. This makes the Swift server quite handy in a microservices infrastructure.

由于不需要Swift编译器来运行服务器,因此Dockerfile会分离构建和运行映像。 因此,Dockerfile的最终输出将非常轻巧。 这使得Swift服务器在微服务基础架构中非常方便。

We can now build and serve the app!

现在,我们可以构建并提供服务了!

docker build -t vapor-image .
docker run --name vapor-server -p 8080:8080 vapor-image

If you navigate to your localhost:8080/hello, you should see the following:

如果导航到localhost:8080 / hello ,则应看到以下内容:

hello world运行_在不到5分钟的时间内运行快速的Hello World服务器_第3张图片

Congratulations! You ran your first Swift hello world server!

恭喜你! 您运行了第一台Swift hello世界服务器!

最后的话 (Final words)

Swift’s community will thrive as more developers join it. We are still at an early adoption phase but thanks to huge projects such as Tensorflow Swift and Fastai, Swift adoption might get a lot of traction. I hope this article inspired some to give Swift a shot. In the future, I am gonna add more content and tutorials regarding Swift and its applications.

随着越来越多的开发人员加入,Swift的社区将蓬勃发展。 我们仍处于早期采用阶段,但是由于Tensorflow Swift和Fastai等大型项目的采用,Swift的采用可能会吸引很多人。 我希望本文能激发一些人对Swift的关注。 将来,我将添加更多有关Swift及其应用程序的内容和教程。

I would love to hear your thoughts about it. So please feel free to share anything with me.

我很想听听您对此的想法。 因此,请随时与我分享任何内容。

Ciao,Michele

乔米歇尔

Here is the link to the repository

这是仓库的链接

翻译自: https://medium.com/phi-skills/run-a-swift-hello-world-server-in-less-than-5-minutes-d35e770d7415

hello world运行

你可能感兴趣的:(python,java,数据库,人工智能,算法)