August 2018
Intermediate to advanced
248 pages
5h 51m
English
There is actually a library, called retrying, that can be used for adding retry behavior to parts of an application. Let's use it for a similar implementation as the one in the first example.
First, make sure you install retrying using the pip install retrying command.
We need to start our code with the following imports:
import timeimport sysimport osfrom retrying import retry
We reuse the same function for creating a file. You could actually externalize it to a common functions module from where you would import it, if you want. For clarity, let's repeat it here:
def create_file(filename, after_delay=5): time.sleep(after_delay) with open(filename, 'w') as f: f.write('A file creation test')