Design and implementation of a RPC library in Audebert python - - PowerPoint PPT Presentation

design and implementation of a rpc library in
SMART_READER_LITE
LIVE PREVIEW

Design and implementation of a RPC library in Audebert python - - PowerPoint PPT Presentation

Design and implementa- tion of a RPC library in python Rmi Design and implementation of a RPC library in Audebert python Introduction Client Service Decorators Signatures Rmi Audebert More signatures Conclusion 2014-07-18 Rmi


slide-1
SLIDE 1

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Design and implementation of a RPC library in python

Rémi Audebert 2014-07-18

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 1 / 32

slide-2
SLIDE 2

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Introduction

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 2 / 32

slide-3
SLIDE 3

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Remote procedure call

RPC Services with methods Clients Request from a client to a service Reply from a service to a client

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 3 / 32

slide-4
SLIDE 4

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Remote procedure call

RPC Services with methods Clients Request from a client to a service Reply from a service to a client RPC system: Cellaserv2 Based on TCP/IP Centralized server Uses protocol buffers More information: https://code.evolutek.org/cellaserv2

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 3 / 32

slide-5
SLIDE 5

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Register + Request + Reply

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 4 / 32

slide-6
SLIDE 6

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

The requierements of this library

Technical Python3 No external libraries Handle Client and Service Usage Easy to use Hard to misuse Fail gracefully Target users: mostly sleep deprived

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 5 / 32

slide-7
SLIDE 7

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

The rush friendly requirement

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 6 / 32

slide-8
SLIDE 8

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Client

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 7 / 32

slide-9
SLIDE 9

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

CellaservProxy

Code from cellaserv.proxy import CellaservProxy client = CellaservProxy() Configuration CellaservProxy(host="example.org", port=4242) Environnment variables: CS_HOST, CS_PORT Configuration file: /etc/conf.d/cellaserv Usage

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 8 / 32

slide-10
SLIDE 10

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

CellaservProxy

Code from cellaserv.proxy import CellaservProxy client = CellaservProxy() Configuration CellaservProxy(host="example.org", port=4242) Environnment variables: CS_HOST, CS_PORT Configuration file: /etc/conf.d/cellaserv Usage now = client.time.date()

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 8 / 32

slide-11
SLIDE 11

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Implementation

Usage client.time.date() cellaserv/proxy.py class CellaservProxy(cellaserv.client.SynClient): ... def __getattr__(self, service_name): return ServiceProxy(self.conn, service_name)

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 9 / 32

slide-12
SLIDE 12

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Implementation

Usage client.time.date() cellaserv/proxy.py class CellaservProxy(cellaserv.client.SynClient): ... def __getattr__(self, "time"): return ServiceProxy(self.conn, "time")

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 10 / 32

slide-13
SLIDE 13

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

client.time.date()

Figure 1: Execution

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 11 / 32

slide-14
SLIDE 14

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Service

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 12 / 32

slide-15
SLIDE 15

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Decorators

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 13 / 32

slide-16
SLIDE 16

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Service’s design

Simple usage import time from cellaserv.service import Service class Time(Service): @Service.action def date(self): return time.time() Time().run()

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 14 / 32

slide-17
SLIDE 17

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Python’s feature: decorators

Simple usage >>> def log_usage(f): ... def wrap(*args, **kwargs): ... print("{} called".format(f)) ... return f(*args, **kwargs) ... return wrap ... >>> my_len = log_usage(len) >>> my_len([42]) <built-in function len> called 1

Note: this decorator should use @functools.wraps(f). Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 15 / 32

slide-18
SLIDE 18

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Service’s design

Advanced usage class Date(Service): @Service.action("heure") @Service.action("time_" + get_timezone()) def time(self): return time.time()

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 16 / 32

slide-19
SLIDE 19

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Function decorator: caution

What is the difference between: Name of the method is name of the function @Service.action def action1(self): pass Name of the method is given by the user @Service.action("action_name") def action2(self): pass

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 17 / 32

slide-20
SLIDE 20

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Function decorator: caution

Name of the method is name of the function action1 = Service.action(action1) Name of the method is given by the user action2 = Service.action("action_name")(action2)

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 18 / 32

slide-21
SLIDE 21

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Python’s feature: decorators

class Service: @staticmethod def action(method_or_name): def _set_action(method, action): try: method._actions.append(action) except AttributeError: method._actions = [action] return method def _wrapper(method): return _set_action(method, method_or_name) if callable(method_or_name): return _set_action(method_or_name, method_or_name.__name__) else: return _wrapper

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 19 / 32

slide-22
SLIDE 22

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Signatures

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 20 / 32

slide-23
SLIDE 23

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Dealing with TypeError

We want to emit a warning when the user send a request with bad arguments.

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 21 / 32

slide-24
SLIDE 24

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Dealing with TypeError

We want to emit a warning when the user send a request with bad arguments. Bad prototype >>> def f(): ... pass >>> f(42) TypeError: f() takes 0 positional arguments but 1 was given

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 21 / 32

slide-25
SLIDE 25

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Dealing with TypeError

We want to emit a warning when the user send a request with bad arguments. Bad prototype >>> def f(): ... pass >>> f(42) TypeError: f() takes 0 positional arguments but 1 was given Bad service code >>> def f(): ... 1 + 'a' >>> f() TypeError: unsupported operand type(s) for +: 'int' and 'str'

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 21 / 32

slide-26
SLIDE 26

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Dealing with TypeError: simple solution

Report all exceptions to the user.

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 22 / 32

slide-27
SLIDE 27

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Checking signatures: quick and dirty solution

Hack Use the stack size. Bad prototype >>> try: ... f(42) ... except: ... print(len(inspect.trace())) 1 Internal error >>> try: ... f() ... except: ... print(len(inspect.trace())) 2

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 23 / 32

slide-28
SLIDE 28

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

PEP-0362: Function Signature Object

The signature object inspect.signature(f) (new in python3.3) Code >>> def f(a, b): ... pass >>> sig = inspect.signature(f) >>> print(sig) (a, b) >>> sig.parameters mappingproxy(OrderedDict([('x', <Parameter at ... 'x'>), ('y', <Parameter at ... 'y'>)]))

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 24 / 32

slide-29
SLIDE 29

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Check signature compatibility?

Use the bind() method >>> def f(a, b): ... pass >>> sig = inspect.signature(f) >>> user_kwargs = {'a': 42, 'b': 1.2} >>> sig.bind(**user_kwargs) <inspect.BoundArguments object at ...>

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 25 / 32

slide-30
SLIDE 30

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Check signature compatibility?

Use the bind() method >>> def f(a, b): ... pass >>> sig = inspect.signature(f) >>> user_kwargs = {'a': 42, 'b': 1.2} >>> sig.bind(**user_kwargs) <inspect.BoundArguments object at ...>

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 25 / 32

slide-31
SLIDE 31

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Bind is smart

Advanced signatures >>> def f(a, *args, v=False, **kwargs): ... pass >>> sig = inspect.signature(f) >>> user_args = ('x', 'y') >>> user_kwargs = {'a': 42, 'v': True} >>> sig.bind(*user_args, **user_kwargs) <inspect.BoundArguments object at ...> >>> sig.bind() TypeError: 'a' parameter lacking default value

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 26 / 32

slide-32
SLIDE 32

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Bind is slow. . .

63 times slower than: Code try: f(**user_kwargs) except TypeError as e: // use inspect.stack() The EAFP coding style Easier to ask for forgiveness than permission. Assume the user is mostly right.

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 27 / 32

slide-33
SLIDE 33

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

More signatures

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 28 / 32

slide-34
SLIDE 34

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Give the user all he needs

PEP-3107: Function Annotations (python3.0) >>> def is_safe(pos: '(x, y) 30mm radius') -> bool: ... pass >>> print(inspect.signature(is_safe)) (pos:'(x, y) 30mm radius') -> bool

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 29 / 32

slide-35
SLIDE 35

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Conclusion

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 30 / 32

slide-36
SLIDE 36

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

More fun in this library

Synchronous and Asynchronous clients Service’s dependencies Events Attributes over RPC Descriptors Identification of services Supports down to python3.1 Manages user’s threads automatically Metaclass . . .

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 31 / 32

slide-37
SLIDE 37

Design and implementa- tion of a RPC library in python Rémi Audebert Introduction Client Service

Decorators Signatures More signatures

Conclusion

Conclusion

This talk Code: http://code.evolutek.eu/python-cellaserv2 Doc: http://doc.evolutek.eu/info/cellaserv.html Discuss: #evolutek<<@irc.rezosup.org Contact IRC: halfr@irc.rezosup.org Mail: halfr@lse.epita.fr Twitter: @halfr

Rémi Audebert Design and implementation of a RPC library in python 2014-07-18 32 / 32