3

I have build a QGIS Plugin which uses a layer with vector tiles (with an attribute location created with gdaltindex) as input to create raster files (slope, hillshades, ...) derived from a DEM for a distinct area. When I choose tiles from another area of interest I remove all raster files first. Logically I can't do this when another QGIS project is open which includes on of these raster layers. In my case I have two projects: an "online" (including network drives) and an "offline" project. With the plugin you have the possibility to update one project with the new created raster layer of the other project via a .qlr file.

So when I want to remove the raster files, there is the error: WindowsError: [Error 32] The process cannot access the file because it is being used by another process: (file-name)

I have solved this using:

try:
    filelist = glob.glob(os.path.join(raster_dest_data, "*.*"))
    for f in filelist:
        os.remove(f)
except:
    QMessageBox.warning(iface.mainWindow(),'Information', u'Quit all other QGIS projects')
    return False

Is this the best way to do this?

Is there the possibility to get information about other open QGIS projects with pyqgis?

I was thinking about a message: There are 2 more QGIS projects open. Please quit QGIS "Project_1" and QGIS "Project_2" first to proceed!

1

I tried some things during the last hours and found a solution:

EDITED*-simplified code

import os
import subprocess
from os.path import basename
from PyQt4.QtGui import QMessageBox

# Hiding subprocess window
SW_HIDE = 0
info = subprocess.STARTUPINFO()
info.dwFlags = subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = SW_HIDE

# Redirect stdin and stderr to NUL.
try:
    from subprocess import DEVNULL
except ImportError:
    DEVNULL = os.open(os.devnull, os.O_RDWR)

# get output from tasklist (-v verbose; tasklist output with window title)
res = subprocess.check_output('C:\Windows\System32\cmd.exe /c tasklist /FI "IMAGENAME eq qgis-bin.exe" -v 2>NUL', stdin=DEVNULL, stderr=DEVNULL, startupinfo = info)

# title of current project where the python code is running
recent_project =os.path.splitext(basename(QgsProject.instance().fileName()))[0]

qgis_procs = []
qgis_list = ['QGIS', 'qgis', 'Qgis']

# creating list of open qgis window titles, except the one where the code is running
for i in res.split('\r\n'):
    if any(word in i for word in qgis_list) and not recent_project in i:
        qgis_procs.append(i.split()[-1]) 

prj_count = len(qgis_procs)
if prj_count > 0:
    if prj_count == 1:
        prj_word1 = 'is'
        prj_word2 = 'project'
    if prj_count > 1:
        prj_word1 = 'are'
        prj_word2 = 'projects'
    QMessageBox.warning(iface.mainWindow(),'Information', u'There ' + prj_word1 + ' ' + str(prj_count) + ' more QGIS ' + prj_word2 + ' open. Please quit the ' + prj_word2 + ' %s' % ' and '.join(map(str, qgis_procs)) + ' to proceed.')
else:
    pass

You can paste this code into your QGIS python console.

For example: When I have 3 QGIS projects open. enter image description here

Any suggestions on how to grep information better from the tasklist output are welcome.

UPDATE

For Linux:

import os
import subprocess
from os.path import basename
from PyQt4.QtGui import QMessageBox

try:
    from subprocess import DEVNULL
except ImportError:
    DEVNULL = os.open(os.devnull, os.O_RDWR)

res = subprocess.check_output(['xwininfo', '-tree', '-root'], stdin=DEVNULL, stderr=DEVNULL)

all_procs = None
all_procs = res.split("\n")
qgis_procs_temp = []

for i in all_procs:
    if "QGIS" in i and "qgis.bin" in i:
        qgis_procs_temp.append(i.split('"')[1].split('-')[1].split())

qgis_procs = [item for sublist in qgis_procs_temp for item in sublist]
prj_count = len(qgis_procs)
base=basename(QgsProject.instance().fileName())
recent_project =[os.path.splitext(base)[0]]
diff_list = [item for item in qgis_procs if item not in recent_project]

if prj_count > 1:
    if prj_count == 2:
        prj_word1 = 'is'
        prj_word2 = 'project'
    if prj_count > 2:
        prj_word1 = 'are'
        prj_word2 = 'projects'
    QMessageBox.warning(iface.mainWindow(),'Information', u'There ' + prj_word1 + ' ' + str(prj_count-1) + ' more QGIS ' + prj_word2 + ' open. Please quit the following ' + prj_word2 + ':' + ' %s' % ' and '.join(map(str, diff_list)) + ' to proceed.')
else:
    pass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.