Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Valery Calderon - Reactive Programming with RxPy

Valery Calderon - Reactive Programming with RxPy

Web applications contains lots of database operations, network calls, nested callbacks and other computationally expensive tasks that might take a long time to complete or even block other threads until it's done, here is where ReactiveX enters, it doesn't only gives us the facility to convert almost anything to a stream; variables, properties, user inputs, caches, etc to manage it asynchronously. But it also gives us an easy way to handle errors which is a hard task within asynchronous programming. ReactiveX makes our code more flexible, readable, maintainable and easy to write.

We will be exploring how ReactiveX help us to make things easier with its operators toolbox that can be used to filter, create, transform or unify any of those streams. We will learn that in just a few lines of maintainable code, we can have multiple web sockets which recieves multiple requests all handled by an asynchronous process that serves a filtered output.

To do that I decided to explain an example of the use with an example by implementing observables, observers/subscribers and subjects. We will start by requesting our data stream from the Github API with a Tornado web socket and then filtering and processing it asynchrounosly.

https://us.pycon.org/2018/schedule/presentation/129/

PyCon 2018

May 11, 2018
Tweet

More Decks by PyCon 2018

Other Decks in Programming

Transcript

  1. $Whoami $ @valerybriz $ Pythonista $ Telecomm Engineer $ Co-Founder

    and Leader of Python Guatemala $ Systems Development Specialist at Skyguard GPS
  2. Data Operators in Reactive Programming • Creating Observables • Transforming

    Observables • Filtering Observables • Combining Observables • Error Handling Operators • Observable Utility Operators • Conditional and Boolean Operators • Mathematical and Aggregate Operators • Connectable Observable Operators
  3. Trying it out self.subject = Subject() user_input = self.subject.throttle_last( 1000

    ).start_with("").filter( lambda text: not text or len(text) > 2 )
  4. Trying it out self.combine_latest_sbs = user_input.combine_latest( interval, lambda input_val, i:

    input_val ).do_action( lambda x: send_response('clear') ).flat_map( self.get_data ).subscribe(send_response, on_error)