pythonReact式编程简介

本文介绍了ReactiveX的基本原理和Python实现RxPY,包括响应式编程的概念、可观察对象、观察者、操作符,以及大理石图和React图的使用。通过示例代码展示了如何使用ReactiveX编写事件驱动的代码,并讨论了错误管理和并发控制。理解这些概念将帮助你开始使用React式编程。
摘要由CSDN通过智能技术生成

ReactiveX is a wonderful framework that allows to write event based code in a very elegant and readable way. Still, getting started in it can be challenging, and intimidating. In practice once you understand few key principles of ReactiveX, you can start writing reactive code easily.

ReactiveX是一个很棒的框架,它允许以非常优雅和可读的方式编写基于事件的代码。 但是,开始使用它可能会充满挑战并且令人生畏。 在实践中,一旦您了解了ReactiveX的几个关键原理,就可以轻松地开始编写React式代码。

The aim of this article is to explain these keys principles, and show how they apply though a simple example. By the way, the first half of this article is language agnostic. So feel free to read it even if you use another programming languages than Python.

本文的目的是解释这些关键原理,并通过一个简单的示例展示它们如何应用。 顺便说一句,本文的前半部分与语言无关。 因此,即使您使用的是Python以外的其他编程语言,也可以随时阅读。

Before reading on, be aware of one important thing: Reactive Programming is addictive ! Once you start thinking as a data flows instead of control flows, you trend to consider that it solves problems better than other programming approaches, and you use reactive programming more and more.

在继续阅读之前,请注意一件事:React式编程会让人上瘾! 一旦开始以数据流而不是控制流的方式思考,就会倾向于认为它比其他编程方法更好地解决了问题,并且越来越多地使用React式编程。

响应式编程和ReactiveX (Reactive Programming and ReactiveX)

So what is reactive programming ? It is a way to write event driven code. The name comes from the fact that a reactive code is composed of entities that react to events being emitted by sources. These entities apply transformations on these events, and return other events as a result. So these entities — named operators — can be chained together, to create computation graphs.

那么什么是React式编程? 这是一种编写事件驱动代码的方法。 该名称源于以下事实:React性代码由对源发出的事件做出React的实体组成。 这些实体对这些事件应用转换,并因此返回其他事件。 因此,可以将这些名为操作符的实体链接在一起,以创建计算图。

Reactive computation graphs are always directed. They flow in only one way. Some graphs are Directed Acyclic Graphs — DAG — like this one:

React性计算图始终是有向的。 它们仅以一种方式流动。 一些图是有向无环图 -DAG-像这样:

Image for post

On this diagram the nodes represent computations, and the edge the link between computations.

在此图上,节点代表计算,边缘代表计算之间的链接。

Some graphs may also be Cycle Graphs like this one:

一些图也可能是像这样的循环图:

Image for post

Cycle graphs are very common when writing a fully reactive application. Most of the time the major part of an application graph is acyclic, and a sub-part may have cycles.

编写完全React性的应用程序时,循环图非常常见。 大多数情况下,应用程序图的主要部分是非循环的,而子部分可能具有循环。

ReactiveX is the most popular implementation of Reactive Programming libraries. One reason is that it was one of the firsts reactive libraries. It was initially developed by Microsoft for the .net platform. Since 2012 the code is open source, and has been ported to more than 20 programming languages.

ReactiveX是Reactive编程库的最流行的实现。 一个原因是它是最早的React式库之一。 它最初是由Microsoft为.net平台开发的。 自2012年以来,该代码是开源的,并已移植到20多种编程语言中。

The python implementation of ReactiveX is RxPY. The library is available on pypi and can be installed with pip:

ReactiveX的python实现是RxPY 。 该库在pypi上可用,可以与pip一起安装:

pip3 install rx

可观察,可观察者,可操作者 (Observable, Observer, Operator)

The foundation of ReactiveX is based on only few key principles described in the

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值