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

Windows: provide APIs to deal with Windows services #795

Closed
giampaolo opened this issue Mar 6, 2016 · 4 comments
Closed

Windows: provide APIs to deal with Windows services #795

giampaolo opened this issue Mar 6, 2016 · 4 comments

Comments

@giampaolo
Copy link
Owner

Rationale

People who want to deal with Windows services in Python are forced to rely either on pywin32 or WMI extensions but they're too low level (pywin32) and a bit confusing IMO. Some examples:

It would be nice if psutil could provide a compact API to deal with Windows services more easily.

API

I'm still not sure how to organize the API (classes? functions? namedtuple? etc.) yet, but roughly the underlying C extension module should expose these APIs:

  • list()
  • create(name, exe_path, start_type='auto', description=None, ...)
  • remove(name)
  • modify(name, description=None, start_type=None, ...)
  • start(name)
  • stop(name)
  • restart(name)

psutil might wrap them by providing 3 higher-level functions + one class:

  • win_service_list() -> return a list or WindowsService classes
  • win_service_install(name, exe_path, start_type='auto', description=None, ...)
  • win_service_get(name) -> return a WindowsService instance with the following attrs/methods:
    • name
    • description
    • start_type
    • status
    • start()
    • stop()
    • restart()
    • modify()
    • remove()

I think it's a good idea to stay as minimal as possible, meaning we should not provide all the features offered by pywin32. Instead we should cover the most common use cases only, which are list/create/modify/remove and start/stop/restart.

References

@giampaolo
Copy link
Owner Author

Implemented and merged as of #803.
Ultimately, I decided not to provide start(), stop() modify() and create(). We only have iter() and get() for now.

@naggie
Copy link

naggie commented Nov 3, 2017

This is great, thanks -- would you consider adding those methods? psutil has such a clean interface, it would be nice to use it for manipulating services too.

@giampaolo
Copy link
Owner Author

Here's the reasons why I didn't expose those methods back then:

psutil/psutil/_pswindows.py

Lines 424 to 440 in 229fcd9

# actions
# XXX: the necessary C bindings for start() and stop() are implemented
# but for now I prefer not to expose them.
# I may change my mind in the future. Reasons:
# - they require Administrator privileges
# - can't implement a timeout for stop() (unless by using a thread,
# which sucks)
# - would require adding ServiceAlreadyStarted and
# ServiceAlreadyStopped exceptions, adding two new APIs.
# - we might also want to have modify(), which would basically mean
# rewriting win32serviceutil.ChangeServiceConfig, which involves a
# lot of stuff (and API constants which would pollute the API), see:
# http://pyxr.sourceforge.net/PyXR/c/python24/lib/site-packages/
# win32/lib/win32serviceutil.py.html#0175
# - psutil is tipically about "read only" monitoring stuff;
# win_service_* APIs should only be used to retrieve a service and
# check whether it's running

@naggie
Copy link

naggie commented Nov 3, 2017

Ok 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants