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
Extend Process.open_files with other FDs #1119
Comments
Hello there and thanks for putting this up. To be honest this doesn't strike me as something consistent or easily portable across platforms. For instance:
...you inferred that the first element is a pipe and the second is a "special" but it's should be the lib which tells you that (also what's a "special" fd?). That poses the disturbing question: what are the available fd types and what do they represent? Are there just "special", "pipe" and "file" or also others? To my knowledge there are many others: fifos, sockets, char and block devices, open directories and who knows what else. Should they be returned? Also, each fd type represents a completely different thing. How should they be exposed to the end user? A utility which managed to wrap this all up is Note: this same proposal has been raised before: #285 |
Hi. Thanks for your time and sharing your thoughts. Good point about #285. I've read it through. Instead of the suggested hard coded types, the user could parse the file's A test where In [ ]: proc = psutil.Process()
In [ ]: proc
Out[ ]: <psutil.Process(pid=38894, name='ipython') at 140000526814512>
In [ ]: def get_type(mode):
...: f_names = [fn for fn in dir(stat) if fn.startswith('S_IS') and callable(getattr(stat, fn))]
...: types = []
...: for name in f_names:
...: func = getattr(stat, name)
...: if func(mode):
...: types.append(name)
...: return types
In [ ]: !ls -l /proc/38894/fd
In [ ]: for fd in os.listdir('/proc/self/fd'):
...: file = '/proc/self/fd/%s' % fd
...: path = os.readlink(file)
...: st = os.stat(file)
...: mode = st.st_mode
...: print(fd, stat.filemode(mode), mode, stat.S_IFMT(mode), get_type(mode), path)
...:
The user could also test whether the The constants in the class Process(object):
"""Linux process implementation."""
# ...
@wrap_exceptions
def open_fds(self, kind=None):
# ...
if kind is None or stat.S_IFMT(st_mode) == kind:
# parse fdinfo, append to ret_list
def open_files(self):
return self.open_fds(kind=stat.S_IFREG)
What do you think? |
Uhm... yes, using "position" (offset) only makes sense for regular files. I see that
"path" field no longer makes sense as a fd can be a socket or something else different than a path.
In summary, I envision this as something along these lines:
One thing which leaves me puzzled is how socket fds should be represented, because they have extra information (local / remote address and status). Right now we already have |
Thanks. Comparing It seems that popenfile(name='/tmp/foo', fd=4, size=292, offset=123, flags=32768, node=2, type="REG") Changing the names of class popenfile(psutil._psplatform.popenfile):
@property
def path(self):
return self.name
@property
def position(self):
return self.sizeoff # or self.offset
... |
Hey @giampaolo I guess there is not a lot of interest in this feature. 😉 |
Sorry I've been travelling and didn't have much time. With that said, I'm not against the idea per se but it looks like a very big task with a hard to define API so let's just say it's low priority for me at this point. I'd say let's just drop it. |
This is a proposal to query all file descriptors separately.
For now, this only works for Linux.
code
benchmark
The following
cat | python
pipeline sleeps in background.Benchmarking in IPython. With the original
_pslinux.Process.open_files()
:After the implementation of
open_fds
and related methods above:Returned values for are the same.
Timings for the new methods:
_pslinux.Process.open_fds()
accepts the followingkind
values for limiting the results (and processing):file
pipe
special
Calling the
_pslinux.Process.open_fds()
without specifying akind
(or through the helper methods), is quite slower, but returns a complete overview of all file descriptors.new uses
We can see - as intended - these processes share the same pipe:
With this information, we can find out which process are attached to a certain pipe. For example, when you want to know which process is attached to the other end of a pipe:
Or more complex:
We could reconstruct the pipeline using this information and a little knowledge of how
cat
opens its files. I am not sure whycat
haspipe:121260469
open in two file descriptors, but whatever. :-)thoughts
psutil.Process
.kind
names;file
should probably beregular
or something similar.kind
as a value to named tuplepopenfile
.What do you think @giampaolo ? Thanks for you consideration.
I will create a PR and unittests if you (and others) think this is useful.
The text was updated successfully, but these errors were encountered: