-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Implemented getpid(p::Process)
.
#24064
Conversation
Get the process ID of a running process. Returns `-1` if the | ||
process is not running. | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about alternatives when the process is not running, like throwing, or returning nothing
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning nothing
would violate type stability. Throwing is an option, but I feel returning -1
is more convenient. There was also a suggestion to save the pid
so that it can be inspected after the process has ended. I don't think this is a good idea because pids can be reused and having getpid(p)
return a potentially unrelated pid is error prone. (E.g. you don't want to accidentally kill an unrelated process.)
Also, as @vtjnash mentioned before, we can take guidance from nodejs where child pid is accessible via a property which does not throw.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran an experiment with node, and it looks like they cache the pid:
> const { spawn } = require('child_process');
> x = spawn('sleep', ['1']);
> x.exitCode
0
> x._handle
null
> x.pid
45660
I still don't think this is a good idea for the reasons I mentioned above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is essentially the same question as #18145. But upon reflection, I think an error would be better here. In some places, -1
is used as a special value to mean "all processes". So kill(getpid(p))
could accidentally cause the system to reboot (surprise!) with this choice of sentinel value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. I'll make the change. Just to make sure: caching the pid is off the table.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nothing
may make it type unstable, but this seems to be the new way of returning what was a Nullable
(ie. returning Union{T, Void}
or Union{Some{T},Void}
), and getpid
is a function for which I would tend to return a Nullable
.
@@ -335,6 +335,20 @@ end | |||
pipe_reader(p::Process) = p.out | |||
pipe_writer(p::Process) = p.in | |||
|
|||
""" | |||
getpid(p::Process) -> Int32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not return Int
?
EDIT: oh I see, getpid()
returns Int32
already, but still...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I just copied that line from the Base.getpid()
doc. The actual type is Cint
, but I think that is the same as Int32
on all the supported platforms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this was the right choice (matching getpid()
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about changing that to Int
for both methods in another PR? for example, one method of kill
(with ClusterManager
) accepts a pid as Int
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rfourquet - isn't that a different ID? I thought kill(::ClusterManager, ..)
takes the worker id, not the OS process id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems you are right.
This seems to have fallen through the cracks, but seems fine to me. @vtjnash ? |
Somewhat tangential to this, but I think that we should use |
Bump |
Rebased. |
Our getpid is actually |
I think it would make sense to have the Libc |
Well, we don't really have a way to do that exactly. Base.getpid and Libc.getpid are either the same function or two functions. |
Oh, I meant make them the same function but put it in the Base namespace rather than Libc |
@IAMED, other packages can already extend it via |
Oh you're right, my bad, that's something good for me to remember in the future. Sorry to muddy the waters. |
Thanks for the PR! Sorry it took us such a long time to land. |
Closes #4752.