Accessing MPI comm in a pvbatch script

I have a python script which reads a .cgns file, computes my quantities of interest and writes it to a file. Initially I was using pvpython to run this script, but as my data sets are growing I wish to switch to pvbatch. When I tried pvbatch everything was working fine but I was receiving output from every MPI rank, i.e.
print("Hello World")
would be printed by every rank, and every rank would write the same value to the file as I did
file.write(value)

Is there a way I could make these commands execute on root rank ? Maybe a pythonic way of accessing MPI comm ? Any help is appreciated.

regards

Prathamesh

To get the process id :

import paraview.servermanager
pm = paraview.servermanager.vtkProcessModule.GetProcessModule()

processId = pm.GetGlobalController().GetLocalProcessId()
if processId == 0:

For more examples take a look into
https://gitlab.kitware.com/paraview/paraview/-/tree/master/Remoting/Application/Testing/Python

And look for examples with “Parallel” in their name.