Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2dafb5d

Browse files
retry loop for file writes
1 parent 45ad14f commit 2dafb5d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

‎src/snippet/config.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Config:
1515
output_append = True # if the output file exists, append to it
1616
output_dir = '.'
1717
output_file_name_template = '{{name}}.md' # a mustache template for the output file name
18+
write_attempts = 3
1819

1920
# Language and style
2021
language_name = 'python'

‎src/snippet/file_wrangler.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import glob
33

44
import pystache
5+
import time
6+
import random
57

68
from snippet.config import Config
79
from snippet.logs import logger
@@ -28,8 +30,18 @@ def write_example(config: Config, example_name, example_block):
2830
os.makedirs(config.output_dir)
2931
output_file = os.path.join(config.output_dir, output_file_name)
3032
logger.info('writing %r to %s', example_name, output_file)
31-
with open(output_file, 'a' if config.output_append else 'w') as fh:
32-
fh.write(output)
33+
for i in range(1, config.write_attempts + 1):
34+
# we run a retry loop as there may be contention on the output file in
35+
# a multi-process environment
36+
try:
37+
with open(output_file, 'a' if config.output_append else 'w') as fh:
38+
fh.write(output)
39+
break
40+
except IOError as err:
41+
time.sleep(i * 0.5 + 0.1 * random.randint(0, 5))
42+
logger.info('write failed (%s) retrying attempt: %s', err, i)
43+
else:
44+
raise IOError('could not write output file after %s attempts' % config.write_attempts)
3345

3446

3547
def load_file_lines(path):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /