Base class for multiprocess applications' clients.
It provides a simple interface to fork a ROOT session into server/worker sessions and exchange messages with them. Multiprocessing applications can build on TMPClient and TMPWorker: the class providing multiprocess functionalities to users should inherit (possibly privately) from TMPClient, and the workers executing tasks should inherit from TMPWorker.
Definition at line 23 of file TMPClient.h.
Public Member Functions | |
TMPClient (const TMPClient &)=delete | |
TMPClient (unsigned nWorkers=0) | |
Class constructor. | |
~TMPClient () | |
Class destructor. | |
template<class T > | |
unsigned | Broadcast (unsigned code, const std::vector< T > &objs) |
Send a message with a different object to each server. | |
template<class T > | |
unsigned | Broadcast (unsigned code, std::initializer_list< T > &objs) |
Send a message with a different object to each server. | |
template<class T > | |
unsigned | Broadcast (unsigned code, T obj, unsigned nMessages=0) |
Send a message containing code and obj to each worker, up to a maximum number of nMessages workers. | |
unsigned | Broadcast (unsigned code, unsigned nMessages=0) |
Send a message with the specified code to at most nMessages workers. | |
void | DeActivate (TSocket *s) |
DeActivate a certain socket. | |
bool | Fork (TMPWorker &server) |
This method forks the ROOT session into fNWorkers children processes. | |
bool | GetIsParent () const |
TMonitor & | GetMonitor () |
unsigned | GetNWorkers () const |
void | HandleMPCode (MPCodeBufPair &msg, TSocket *sender) |
Handle messages containing an EMPCode. | |
TMPClient & | operator= (const TMPClient &)=delete |
void | ReapWorkers () |
Wait on worker processes and remove their pids from fWorkerPids. | |
void | Remove (TSocket *s) |
Remove a certain socket from the monitor. | |
void | SetNWorkers (unsigned n) |
Set the number of workers that will be spawned by the next call to Fork() | |
Private Attributes | |
bool | fIsParent |
This is true if this is the parent/client process, false if this is a child/worker process. | |
TMonitor | fMon |
This object manages the sockets and detect socket events via TMonitor::Select. | |
unsigned | fNWorkers |
The number of workers that should be spawned upon forking. | |
std::vector< pid_t > | fWorkerPids |
A vector containing the PIDs of children processes/workers. | |
#include <TMPClient.h>
|
explicit |
Class constructor.
nWorkers | the number of children processes that will be created by Fork, i.e. the number of workers that will be available after this call. The default value (0) means that a number of workers equal to the number of cores of the machine is going to be spawned. If that information is not available, 2 workers are created instead. |
Definition at line 51 of file TMPClient.cxx.
TMPClient::~TMPClient | ( | ) |
Class destructor.
This method is in charge of shutting down any remaining worker, closing off connections and reap the terminated children processes.
Definition at line 70 of file TMPClient.cxx.
|
delete |
unsigned TMPClient::Broadcast | ( | unsigned | code, |
const std::vector< T > & | args | ||
) |
Send a message with a different object to each server.
Sockets can either be in an "active" or "non-active" state. This method activates all the sockets through which the client is connected to the workers, and deactivates them when a message is sent to the corresponding worker. This way the sockets pertaining to workers who have been left idle will be the only ones in the active list (TSocket::GetMonitor()->GetListOfActives()) after execution.
code | the code of the message to send (e.g. EMPCode) |
args | a vector containing the different messages to be sent. If the size of the vector is smaller than the number of workers, a message will be sent only to the first args.size() workers. If the size of the args vector is bigger than the number of workers, only the first fNWorkers arguments will be sent. |
Definition at line 73 of file TMPClient.h.
unsigned TMPClient::Broadcast | ( | unsigned | code, |
std::initializer_list< T > & | args | ||
) |
Send a message with a different object to each server.
See TMPClient::Broadcast(unsigned code, const std::vector<T> &args) for more informations.
Definition at line 100 of file TMPClient.h.
unsigned TMPClient::Broadcast | ( | unsigned | code, |
T | obj, | ||
unsigned | nMessages = 0 |
||
) |
Send a message containing code and obj to each worker, up to a maximum number of nMessages workers.
See Broadcast(unsigned code, unsigned nMessages) for more informations.
code | the code of the message to send (e.g. EMPCode) |
obj | the object to send |
nMessages | the maximum number of messages to send. If nMessages == 0, send a message to every worker. |
Definition at line 120 of file TMPClient.h.
unsigned TMPClient::Broadcast | ( | unsigned | code, |
unsigned | nMessages = 0 |
||
) |
Send a message with the specified code to at most nMessages workers.
Sockets can either be in an "active" or "non-active" state. This method activates all the sockets through which the client is connected to the workers, and deactivates them when a message is sent to the corresponding worker. This way the sockets pertaining to workers who have been left idle will be the only ones in the active list (TSocket::GetMonitor()->GetListOfActives()) after execution.
code | the code to send (e.g. EMPCode) |
nMessages | the maximum number of messages to send. If nMessages == 0 || nMessage > fNWorkers , send a message to every worker. |
Definition at line 253 of file TMPClient.cxx.
void TMPClient::DeActivate | ( | TSocket * | s | ) |
DeActivate a certain socket.
This does not remove it from the monitor: it will be reactivated by the next call to Broadcast() (or possibly other methods that are specified to do so).
A socket should be DeActivated when the corresponding worker is done for now and we want to stop listening to this worker's socket. If the worker is done forever, Remove() should be used instead.
s | the socket to be deactivated |
Definition at line 286 of file TMPClient.cxx.
This method forks the ROOT session into fNWorkers children processes.
The ROOT sessions spawned in this way will not have graphical capabilities and will not read from standard input, but will be connected to the original (interactive) session through TSockets. The children processes' PIDs are added to the fWorkerPids vector. The parent session can then communicate with the children using the Broadcast and MPSend methods, and receive messages through MPRecv.
server | A pointer to an instance of the class that will take control of the subprocesses after forking. Applications should implement their own class inheriting from TMPWorker. Behaviour can be customized overriding TMPWorker::HandleInput. |
Definition at line 128 of file TMPClient.cxx.
|
inline |
Definition at line 37 of file TMPClient.h.
|
inline |
Definition at line 36 of file TMPClient.h.
|
inline |
Definition at line 40 of file TMPClient.h.
void TMPClient::HandleMPCode | ( | MPCodeBufPair & | msg, |
TSocket * | s | ||
) |
Handle messages containing an EMPCode.
This method should be called upon receiving a message with a code >= 1000 (i.e. EMPCode). It handles the most generic types of messages.
Classes inheriting from TMPClient should implement a similar method to handle message codes specific to the application they're part of.
msg | the MPCodeBufPair returned by a MPRecv call |
s | a pointer to the socket from which the message has been received is passed. This way HandleMPCode knows which socket to reply on. |
Definition at line 334 of file TMPClient.cxx.
void TMPClient::ReapWorkers | ( | ) |
Wait on worker processes and remove their pids from fWorkerPids.
A blocking waitpid is called, but this should actually not block execution since ReapWorkers should only be called when all workers have already quit. ReapWorkers is then called not to leave zombie processes hanging around, and to clean-up fWorkerPids.
Definition at line 313 of file TMPClient.cxx.
void TMPClient::Remove | ( | TSocket * | s | ) |
Remove a certain socket from the monitor.
A socket should be Removed from the monitor when the corresponding worker is done forever. For example HandleMPCode() calls this method on sockets pertaining to workers which sent an MPCode::kShutdownNotice.
If the worker is done for now, DeActivate should be used instead.
s | the socket to be removed from the monitor fMon |
Definition at line 300 of file TMPClient.cxx.
|
inline |
Set the number of workers that will be spawned by the next call to Fork()
Definition at line 39 of file TMPClient.h.
|
private |
This is true if this is the parent/client process, false if this is a child/worker process.
Definition at line 47 of file TMPClient.h.
|
private |
This object manages the sockets and detect socket events via TMonitor::Select.
Definition at line 49 of file TMPClient.h.
|
private |
The number of workers that should be spawned upon forking.
Definition at line 50 of file TMPClient.h.
|
private |
A vector containing the PIDs of children processes/workers.
Definition at line 48 of file TMPClient.h.