Skip to Content
Mastering Python Design Patterns - Second Edition
book

Mastering Python Design Patterns - Second Edition

by Kamon Ayeva, Sakis Kasampalis
August 2018
Intermediate to advanced
248 pages
5h 51m
English
Packt Publishing
Content preview from Mastering Python Design Patterns - Second Edition

Implementation

We are going to use the pybreaker library. You can install it in your Python, via the pip install pybreaker command.

Let's say you want to use a circuit breaker on a flaky function (for example, fragile due to the networking environment it depends on).

Our implementation is inspired by a nice script I found in this repository: https://github.com/veltra/pybreaker-playground, which we will adapt.

Here is our version of the function to simulate fragile calls:

def fragile_function():    if not random.choice([True, False]):        print(' / OK', end='')    else:        print(' / FAIL', end='')        raise Exception('This is a sample Exception')

Let's define our cirbuit breaker to automatically open the circuit after five consecutive failures in that function. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Python Design Patterns - Second Edition - Second Edition

Learning Python Design Patterns - Second Edition - Second Edition

Chetan Giridhar, Gennadiy Zlobin

Publisher Resources

ISBN: 9781788837484Supplemental Content