Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on machines without a display #44

Conversation

willgittoes-dd
Copy link
Contributor

The bug is, when I try and plot with psrecord on a headless machine (with no displays), then I get the following stack trace:

Traceback (most recent call last):
  File "/usr/local/bin/psrecord", line 7, in <module>
    sys.exit(psrecord.main())
  File "/usr/local/lib/python2.7/dist-packages/psrecord/main.py", line 108, in main
    interval=args.interval, include_children=args.include_children)
  File "/usr/local/lib/python2.7/dist-packages/psrecord/main.py", line 214, in monitor
    fig = plt.figure()
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 539, in figure
    **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 171, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 1049, in new_figure_manager_given_figure
    window = Tk.Tk(className="matplotlib")
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1822, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

This can be fixed by using non-interactive MPL backend, as far as I can tell there's no need for psrecord to use the interactive one since it's just generating images. Since it's creating pngs only at the moment, using a raster backend is fine.

If there's a reason you'd rather not use 'Agg' by default, then perhaps I can instead add at least a try-catch that prints an instructive message about setting the matplotlib backend via environment variable.

psrecord/main.py Outdated
@@ -209,6 +209,9 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None,

if plot:

# Use non-interactive backend, to enable operation on headless machines.
import matplotlib as mpl
mpl.use('Agg')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than changing it globally, just in case anyone uses psrecord from within a Python/IPython session, could you instead use a context manager, i.e.

with plt.rc_context({'backend': 'Agg'}):
    import matplotlib.pyplot as plt
    # plotting code

Thanks!

@willgittoes-dd willgittoes-dd force-pushed the willgittoes-dd/headless-operation-by-default branch from c99c262 to 1de8214 Compare September 6, 2018 06:44
Copy link
Owner

@astrofrog astrofrog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks! The failure is unrelated

@astrofrog astrofrog merged commit e604aec into astrofrog:master Sep 6, 2018
@willgittoes-dd willgittoes-dd deleted the willgittoes-dd/headless-operation-by-default branch September 6, 2018 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants