Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
mantid
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mantidproject
mantid
Commits
6766f149
Commit
6766f149
authored
6 years ago
by
Martyn Gigg
Committed by
Gigg, Martyn Anthony
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add function to retrieve other process ids using psutil
Refs #0
parent
d49f2ac2
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
MantidPlot/CMakeLists.txt
+2
-0
2 additions, 0 deletions
MantidPlot/CMakeLists.txt
MantidPlot/src/Process.cpp
+111
-0
111 additions, 0 deletions
MantidPlot/src/Process.cpp
MantidPlot/src/Process.h
+36
-0
36 additions, 0 deletions
MantidPlot/src/Process.h
with
149 additions
and
0 deletions
MantidPlot/CMakeLists.txt
+
2
−
0
View file @
6766f149
...
@@ -91,6 +91,7 @@ set ( QTIPLOT_SRCS src/ApplicationWindow.cpp
...
@@ -91,6 +91,7 @@ set ( QTIPLOT_SRCS src/ApplicationWindow.cpp
src/PluginFit.cpp
src/PluginFit.cpp
src/PolynomFitDialog.cpp
src/PolynomFitDialog.cpp
src/PolynomialFit.cpp
src/PolynomialFit.cpp
src/Process.cpp
src/ProjectRecovery.cpp
src/ProjectRecovery.cpp
src/ProjectSaveView.cpp
src/ProjectSaveView.cpp
src/ProjectSerialiser.cpp
src/ProjectSerialiser.cpp
...
@@ -291,6 +292,7 @@ set ( QTIPLOT_HDRS src/ApplicationWindow.h
...
@@ -291,6 +292,7 @@ set ( QTIPLOT_HDRS src/ApplicationWindow.h
src/PluginFit.h
src/PluginFit.h
src/PolynomFitDialog.h
src/PolynomFitDialog.h
src/PolynomialFit.h
src/PolynomialFit.h
src/Process.h
src/ProjectRecovery.h
src/ProjectRecovery.h
src/ProjectSerialiser.h
src/ProjectSerialiser.h
src/ProjectSaveView.h
src/ProjectSaveView.h
...
...
This diff is collapsed.
Click to expand it.
MantidPlot/src/Process.cpp
0 → 100644
+
111
−
0
View file @
6766f149
// clang-format off
#include
"MantidQtWidgets/Common/PythonThreading.h"
// clang-format on
#include
"Process.h"
#include
<iostream>
#include
<stdexcept>
#include
<QCoreApplication>
namespace
{
class
PyObjectNewReference
{
public:
PyObjectNewReference
(
PyObject
*
object
)
:
m_object
(
object
)
{}
~
PyObjectNewReference
()
{
Py_XDECREF
(
m_object
);
}
PyObjectNewReference
(
const
PyObjectNewReference
&
)
=
delete
;
PyObjectNewReference
&
operator
=
(
const
PyObjectNewReference
&
)
=
delete
;
PyObjectNewReference
(
PyObjectNewReference
&&
)
=
default
;
PyObjectNewReference
&
operator
=
(
PyObjectNewReference
&&
)
=
default
;
inline
PyObject
*
ptr
()
const
{
return
m_object
;
}
private
:
PyObject
*
m_object
;
};
/**
* @brief Retrieve a named attribute
* @param source The source object
* @param name The name of the attribute
* @return The attribute
* @throws std::runtime_error if an error occurs retrieving the attribute
*/
PyObjectNewReference
attr
(
PyObject
*
source
,
const
char
*
name
)
{
PyObjectNewReference
attr
(
PyObject_GetAttrString
(
source
,
name
));
if
(
attr
.
ptr
())
{
return
attr
;
}
else
{
PyErr_Clear
();
throw
std
::
runtime_error
(
std
::
string
(
"Process: No attribute "
)
+
name
+
" found"
);
}
}
/**
* @brief Call a named function with an check for errors
* @param source The source object
* @param name The name of the attribute to call
* @return The return value of the function
* @throws std::runtime_error if an error occurs retrieving the attribute
*/
PyObjectNewReference
call
(
PyObject
*
source
,
const
char
*
name
)
{
auto
result
=
PyObject_CallFunction
(
attr
(
source
,
name
).
ptr
(),
nullptr
);
if
(
result
)
return
PyObjectNewReference
(
result
);
else
{
PyErr_Clear
();
throw
std
::
runtime_error
(
std
::
string
(
"Process: Error calling function "
)
+
name
);
}
}
/**
* @return Return a pointer to the psutil module. A new reference is returned.
*/
PyObjectNewReference
psutil
()
{
if
(
auto
process
=
PyImport_ImportModule
(
"psutil"
))
{
return
PyObjectNewReference
(
process
);
}
else
{
PyErr_Clear
();
throw
std
::
runtime_error
(
"Python module psutil cannot be imported."
);
}
}
}
// namespace
namespace
Process
{
/**
* @brief Return a list of process IDs for other instances of this process.
* @return A list of other processes running. The PID for this process is
* removed from the list. An empty list is returned
* if no other processes are running.
* @throws std::runtime_error if the PID list cannot be determined
*/
std
::
vector
<
int64_t
>
otherInstancePIDs
()
{
ScopedPythonGIL
lock
;
const
int64_t
ourPID
(
QCoreApplication
::
applicationPid
());
const
PyObjectNewReference
ourName
(
PyString_FromString
(
QCoreApplication
::
applicationName
().
toLatin1
().
data
()));
auto
psutilModule
(
psutil
());
auto
processIter
(
call
(
psutilModule
.
ptr
(),
"process_iter"
));
std
::
vector
<
int64_t
>
otherPIDs
;
PyObject
*
item
(
nullptr
);
while
((
item
=
PyIter_Next
(
processIter
.
ptr
())))
{
auto
name
=
call
(
item
,
"name"
);
if
(
PyObject_RichCompareBool
(
name
.
ptr
(),
ourName
.
ptr
(),
Py_EQ
))
{
auto
pid
=
PyLong_AsLong
(
attr
(
item
,
"pid"
).
ptr
());
if
(
pid
!=
ourPID
)
{
otherPIDs
.
emplace_back
(
pid
);
}
}
Py_DECREF
(
item
);
}
return
otherPIDs
;
}
}
// namespace Process
This diff is collapsed.
Click to expand it.
MantidPlot/src/Process.h
0 → 100644
+
36
−
0
View file @
6766f149
#ifndef PROCESS_H_
#define PROCESS_H_
/*
Copyright © 2008-18 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
National Laboratory & European Spallation Source
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include
<cstdint>
#include
<vector>
/*
* A minimal wrapper around Python's psutil package to gather information
* about processes
*/
namespace
Process
{
std
::
vector
<
int64_t
>
otherInstancePIDs
();
}
#endif // PROCESS_H_
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment