dealing with python reactively - pycon korea 2017

32
Dealing with Python Reactively PyCon Korea 2017 Kenneth

Upload: kenneth-ceyer

Post on 15-Mar-2018

345 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Dealing with Python Reactively - PyCon Korea 2017

Dealing with Python Reactively PyCon Korea 2017

Kenneth

Page 2: Dealing with Python Reactively - PyCon Korea 2017

SPEAKER

Editor

Dev team leader

Kenneth

Page 3: Dealing with Python Reactively - PyCon Korea 2017

INTRODUCTION

Ractive Progamming

RxPy

Coroutine / Generator

CH1.

CH2.

CH3.

Page 4: Dealing with Python Reactively - PyCon Korea 2017

CH1.

Reactive Programming

Page 5: Dealing with Python Reactively - PyCon Korea 2017

What is reactive programming?

CH 1. Reactive Programming

The new paradigm that focused on the asynchronous data flow

Page 6: Dealing with Python Reactively - PyCon Korea 2017

Difficult

Page 7: Dealing with Python Reactively - PyCon Korea 2017

Key keyword #1

CH 1. Reactive Programming

Asynchronous It can be received the data that is not guaranteed

In the asynchronous environment sequentially, And also it can be processed the data flexible.

Page 8: Dealing with Python Reactively - PyCon Korea 2017

Key keyword #2

CH 1. Reactive Programming

Reactive It is not executed if there is no operation (input) from the outside.

Page 9: Dealing with Python Reactively - PyCon Korea 2017

Key keyword #3

CH 1. Reactive Programming

Lightweight Data flow can be subdivided for each purpose,

And also it can be merged again. This makes it possible to reduce the weight.

Page 10: Dealing with Python Reactively - PyCon Korea 2017

The data flows.

CH 1. Reactive Programming

Page 11: Dealing with Python Reactively - PyCon Korea 2017

However it does not flow at the same time. So we can not guarantee that the data always comes.

CH 1. Reactive Programming

Page 12: Dealing with Python Reactively - PyCon Korea 2017

It can not be easy to process the data that comes every another times.

CH 1. Reactive Programming

.next() .subscribe()

Page 13: Dealing with Python Reactively - PyCon Korea 2017

Controlling multiple data flows. Reactive programming makes it possible.

CH 1. Reactive Programming

The data that comes from each different times concurrently,

To be sequentially

Page 14: Dealing with Python Reactively - PyCon Korea 2017

It only see the data flows. That’s why it’s intuitive.

CH 1. Reactive Programming

Map

Filter

Merge Reduce

On Complete

On Error Retry

Skip Buffer

Page 15: Dealing with Python Reactively - PyCon Korea 2017

CH2.

RxPy

Page 16: Dealing with Python Reactively - PyCon Korea 2017

ReactiveX (RX)

CH 2. RxPy

Microsoft announced `Volta` project in 2007

It is officially known as `Reactive Extensions` in 2009

It was gradually released as open source since 2012

Page 17: Dealing with Python Reactively - PyCon Korea 2017

def observer_generator(observer): # It passes the string “hello” through the observer. observer.on_next("hello") # Likewise, it passes the string "world!" through the observer. observer.on_next("world!") def main(): # Create an observer, passes it to a predefined function, and receives an object that can receive it. observable = Observable.create(observer_generator) # Receive the observer, At this time the observer read the variable that passed at on_next. # Oh! After the below subscribe is started, the above observer_generator will be executed. observable.subscribe(on_next=lambda value: print(value))

hello_world.py

observable.create (Create an observer)

observer_generator

observer

Page 18: Dealing with Python Reactively - PyCon Korea 2017

def observer_generator(observer): # It passes the string “hello” through the observer. observer.on_next("hello") # Likewise, it passes the string "world!" through the observer. observer.on_next("world!") def main(): # Create an observer, passes it to a predefined function, and receives an object that can receive it. observable = Observable.create(observer_generator) # Receive the observer, At this time the observer read the variable that passed at on_next. # Oh! After the below subscribe is started, the above observer_generator will be executed. observable.subscribe(on_next=lambda value: print(value))

hello_world.py

observable

1. next(‘hello’)

2. next(‘world!’)

observer

1. print(‘hello’)

2. print(‘world!’)

It passes the data through on_next

Print the message that received from on_next

Page 19: Dealing with Python Reactively - PyCon Korea 2017

from rx import Observable, Observer class PrintObserver(Observer): def on_next(self, value): print('on_next value:%s’ % (value)) def on_completed(self): print('on_completed !') def on_error(self, value): print('on_error value:%s’ % (value)) def observer_generator(observer): observer.on_next(“break") observer.on_next(“the ice!") while True: message = input() if message: observer.on_next(message) else: observer.on_completed() break def main(): observable = Observable.create(observer_generator) observable.subscribe(PrintObserver())

ice_breaking.py

Observable (Data forwarder)

Observable (Data receiver)

subscribe()

next(‘break’)

next(‘the ice!’)

next(‘message’)

print()

print()

print()

on_next

on_next

on_next

completed()

on_completed print()

1. You can expand incoming messages by using Predefined object in Subscribe method.

Page 20: Dealing with Python Reactively - PyCon Korea 2017

CH3.

Coroutine / Generator

Page 21: Dealing with Python Reactively - PyCon Korea 2017

Coroutine?

CH 3. Coroutine / Generator

Unlike functions, Routines whose parents are

“equivalent” to the called function.

Python only: Coroutine can process only the received data.

Page 22: Dealing with Python Reactively - PyCon Korea 2017

Coroutine vs General routine

CH 3. Coroutine / Generator

General Routine

Function call

Return

Parameters

Result

Coroutine

Function call

Parameters

Yield

Yield

Yield

Main code Main code

Calculating

Calculating

Calculating

Send

Send

Send

Page 23: Dealing with Python Reactively - PyCon Korea 2017

Use Case

CH 3. Coroutine / Generator

Init Data

Caller Coroutine

2. Wait for the caller's input via yield (Will be returned to caller code lines)

1. Inserting initial data to apply to coroutines

3. If you have input to the caller, return to the coroutine code and execute the logic.

If yield appears in the logic, it returns to the parent code again.

(repeat)

next()

Yield

Yield

4. Finally, the caller ends the coroutine. Close

Page 24: Dealing with Python Reactively - PyCon Korea 2017

Generator?

CH 3. Coroutine / Generator

If Coroutine is a gourmand, The Generator is the giving tree.

A generator is a `generator` that generates data through yield.

Page 25: Dealing with Python Reactively - PyCon Korea 2017

Do you know range function?

CH 3. Coroutine / Generator

def main(): # Use the range function to insert each value from 0 to 2 # into value and repeat it three times. for value in range(3): print(u’current value %d' % (value)) OUTPUT: current_value 0 current_value 1 current_value 2

Page 26: Dealing with Python Reactively - PyCon Korea 2017

Making range function by using Generator.

CH 3. Coroutine / Generator

# It is the range function created by using the Generator. def custom_range(number): index = 0 while(index < number): # At this point, we go back to the parent that called this function, # Pass the value, and proceed to the parent's logic until we call this function again. # This is the heart of the generator. Remember this! yield index index += 1

Page 27: Dealing with Python Reactively - PyCon Korea 2017

coroutine_generator.py

def main(): # Let's try to use the existing range function. for value in range(3): print(u'original range %d' % (value)) # Insert a line escaping for the division. print(u'\n') # Let's try the function we just created. for value in custom_range(3): print(u'custom range %d' % (value))

OUTPUT original range 0 original range 1 original range 2 custom range 0 custom range 1 custom range 2

Page 28: Dealing with Python Reactively - PyCon Korea 2017

Use Case

CH 3. Coroutine / Generator

Large Data

Memory

Process

yield

1. In a real-time cursor, yield returns

every 500 datasets.

2. Actually there are only 500 data in memory, so there is out of memory problem.

3. When 500 data are processed and the process is finished,

500 data will be emptied from the memory. Likewise, there is no out of memory problem.

4. At the end of the process, it again gets 500 data from Large Data.

Page 29: Dealing with Python Reactively - PyCon Korea 2017

Conclusion about Generator

CH 3. Coroutine / Generator

Data that comes in real time can be used to stabilize

the memory by using the generator! (Performance difference is insignificant)

Page 30: Dealing with Python Reactively - PyCon Korea 2017

What is the difference Between Coroutine and Generator?

CH 3. Coroutine / Generator

Corutine

Function call

Parameters

Yield

Yield

Yield

Main code

Calculating

Calculating

Calculating

Send

Send

Generator

Function call

Parameters

Yield

Yield

Yield

Main code

Calculating

Calculating

Calculating

Return

Return

Return

Send

Page 31: Dealing with Python Reactively - PyCon Korea 2017

Question

Page 32: Dealing with Python Reactively - PyCon Korea 2017

Thank you

[email protected] http://github.com/KennethanCeyer/pycon-kr-2017

Email GitHub