Skip to content

Commit

Permalink
Add timeout for downloading tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Jekus committed Aug 10, 2016
1 parent 8a70f99 commit 4cd90d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions himawaripy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
appauthor=False),
"latest.png")

# Deadline for the whole download process in minutes
dl_deadline = 6

# Xfce4 displays to change the background of
xfce_displays = ["/backdrop/screen0/monitor0/image-path",
"/backdrop/screen0/monitor0/workspace0/last-image"]
17 changes: 13 additions & 4 deletions himawaripy/himawaripy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
from pytz import timezone
from tzlocal import get_localzone

from .config import level, output_file, auto_offset, hour_offset
from .config import level, output_file, auto_offset, hour_offset , dl_deadline
from .utils import set_background, get_desktop_environment

counter = None
height = 550
width = 550
dl_timeout = dl_deadline * 60 / (level ** 2)


def get_time_offset(latest_date):
Expand Down Expand Up @@ -49,9 +50,13 @@ def download_chunk(args):

x, y, latest = args
url_format = "http://himawari8.nict.go.jp/img/D531106/{}d/{}/{}_{}_{}.png"
url = url_format.format(level, width, strftime("%Y/%m/%d/%H%M%S", latest), x, y)

with urlopen(url_format.format(level, width, strftime("%Y/%m/%d/%H%M%S", latest), x, y)) as tile_w:
tiledata = tile_w.read()
try:
with urlopen(url , timeout = dl_timeout) as tile_w:
tiledata = tile_w.read()
except:
raise OSError("\n\nTimeout when downloading tiles.")

with counter.get_lock():
counter.value += 1
Expand Down Expand Up @@ -85,7 +90,11 @@ def main():
counter = Value("i", 0)
p = Pool(cpu_count() * level)
print("Downloading tiles: 0/{} completed".format(level * level), end="", flush=True)
res = p.map(download_chunk, product(range(level), range(level), (requested_time,)))
try:
res = p.map(download_chunk, product(range(level), range(level), (requested_time,)))
except OSError as oe:
p.terminate()
exit(oe)

for (x, y, tiledata) in res:
tile = Image.open(BytesIO(tiledata))
Expand Down

0 comments on commit 4cd90d2

Please sign in to comment.