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

Add Windows support to /scripts/top.py #1371

Closed
wants to merge 6 commits into from

Conversation

jdgregson
Copy link

While I realize that the files in /scripts are examples and not critical parts of the project, I feel that it's important for examples of a cross-platform library to themselves be cross-platform. This PR addresses the following Windows compatibility issues with /scripts/top.py:

  • os.getloadavg() is not available on Windows, causing the script to throw an unhandled exception.
  • Nice values are provided as strings like 'None' and 'Priority.NORMAL_PRIORITY_CLASS'.
  • Control+C is implemented differently on Windows which prevents the user form terminating the script.
  • As a bonus, 'q' to quit has been implemented on Windows and Linux.

On Windows with Python 3, os does not have a 'getloadavg' method. To
work around this, we should simply omit the "Load average" field from
the header if the 'getloadavg' is not found.
Windows uses a priroity like 'Normal' to communicate a process's
niceness rather than a number. When running top.py on Windows, the
niceness was coming through as a Priority enum which was cast to
a string like 'Priority.NORMAL_PRIORITY_CLASS', completely destroying
the column widths.

This commit addresses the issue like so:
  - A new function 'niceness2priority' was added which will return a
    more output-firendly word like 'Normal' or 'Idle' on Windows and
    the niceness on Linux.
  - The NI column will become the PRI column on Windows.
On Windows systems, Ctrl+C is implemented differently, and does not
allow the user to terminate the application. This commit addresses
the issue by adding a thread which watches for user input and exits
the application if 'q' or Ctrl+C is entered.

Tested in:
  - Python 3.6 (Windows)
  - Python 3.6 (Linux)
  - Python 2.7 (Linux)
Commit 339349e failed to allow 'q' and Ctrl+C to exit on Windows in
new terminals. Once the python process was terminated, subsequent
executions of the application were able to exit on user input as
expected, which is why it was missed in testing. The issue did not
affect Linux.

This commit adds better 'q' and Ctrl+C to quit support for Windows
and Linux.
@giampaolo
Copy link
Owner

Hello. scripts/top.py is not supposed to work on Windows because curses module is not available (for me at least). Are you able to import curses module on Windows?

@jdgregson
Copy link
Author

@giampaolo Yes, on Windows 10 with Python 3.7 installed, I am able to run pip install windows-curses and then import curses works as expected.

@giampaolo
Copy link
Owner

(looking back at this 4 years later)

I'm going to reject this because it adds too much complexity for little value (IMO). Scripts in scripts/*.py are intended to show how some APIs can be used and not much more than that. I prefer to keep them simple at the cost of sacrificing Windows support.

@giampaolo giampaolo closed this Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants