Skip to content

Commit 6f7f92f

Browse files
committed
call plotly's write_image in timed process to prevent hanging
1 parent 1f1f54f commit 6f7f92f

1 file changed

Lines changed: 51 additions & 12 deletions

File tree

gnss_lib_py/utils/visualizations.py

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import os
99
import pathlib
10+
from multiprocessing import Process
1011

1112
import numpy as np
1213
import pandas as pd
@@ -411,7 +412,7 @@ def plot_map(*args, sections=0, save=False, prefix="",
411412
be overwritten.
412413
width : int
413414
Figure width in pixels.
414-
height = int
415+
height : int
415416
Figure height in pixels.
416417
mapbox_style : str
417418
Can optionally be included as one of the ``**kwargs``
@@ -838,7 +839,7 @@ def _save_plotly(figures, titles=None, prefix="", fnames=None,
838839
be overwritten.
839840
width : int
840841
Figure width in pixels.
841-
height = int
842+
height : int
842843
Figure height in pixels.
843844
844845
"""
@@ -873,15 +874,53 @@ def _save_plotly(figures, titles=None, prefix="", fnames=None,
873874
+ ".png")
874875
else:
875876
fname = fnames[fig_idx]
877+
876878
while True:
877-
try:
878-
figure.write_image(fname,
879-
width = width,
880-
height = height,
881-
)
879+
# sometimes writing a plotly image hanges for an unknown
880+
# reason. Hence, we call write_image in a process that is
881+
# automatically terminated after 180 seconds if nothing
882+
# happens.
883+
process = Process(target=_write_plotly,
884+
name="write_plotly",
885+
args=(figure,fname,width,height))
886+
process.start()
887+
888+
process.join(180)
889+
if process.is_alive():
890+
process.terminate()
891+
process.join()
892+
continue
893+
break
894+
895+
896+
def _write_plotly(figure, fname, width, height): # pragma: no cover
897+
"""Saves figure to file.
898+
899+
Automatically zooms out if plotly throws a ValueError when trying
900+
to zoom in too much.
901+
902+
Parameters
903+
----------
904+
figure : plotly.graph_objects.Figure
905+
Object to save.
906+
fname : string or path-like
907+
Path to save figure to.
908+
width : int
909+
Figure width in pixels.
910+
height : int
911+
Figure height in pixels.
912+
913+
"""
914+
915+
while True:
916+
try:
917+
figure.write_image(fname,
918+
width = width,
919+
height = height,
920+
)
921+
break
922+
except ValueError as error:
923+
figure.layout.mapbox.zoom -= 1
924+
if figure.layout.mapbox.zoom < 1:
925+
print(error)
882926
break
883-
except ValueError as error:
884-
figure.layout.mapbox.zoom -= 1
885-
if figure.layout.mapbox.zoom < 1:
886-
print(error)
887-
break

0 commit comments

Comments
 (0)