I have some PyQGIS code to create multiple layers in a QGIS project using a for loop. This code previously worked for me on Windows, but now I am on Linux and run into the following problem:

ERROR 4: sqlite3_open(/path/to/data_source.gpkg) failed: unable to open database file
ERROR 1: unable to open database file
More than 1000 errors or warnings have been reported. No more will be reported from now.
Critical: Could not create AF_NETLINK socket (Too many open files)
(qgis:27944): GLib-ERROR **: 00:05:45.770: Creating pipes for GWakeup: Too many open files
QGIS died on signal 5ptrace: Operation not permitted.
No thread selected
No stack.
gdb returned 256

I have (re)created the data source geopackage file on this system without problems, so QGIS is evidently able create and read the .gpkg file. Is there any problem in the following code that explains this behavior? Or ideas for what might be wrong with my system/environment/QGIS installation?

from qgis.PyQt.QtCore import *

root = project.layerTreeRoot()
myPatternsGroup = root.findGroup('My Patterns')
template_layer = QgsProject.instance().mapLayersByName('Example layer')[0]

with open('/path/to/patterns.txt', 'rt', encoding='utf-8') as file:
    lines = file.read().splitlines()
    for pattern in lines:
        clone = template_layer.clone()
        clone.setSubsetString(f""""field_name" LIKE '%{pattern}%'""")
        QgsProject.instance().addMapLayer(clone, False)
        newLayer = myPatternsGroup.addLayer(clone)
  • First thing to check is does /path/to/data_source.gpkg actually exist? and then do you have read, write and execute permissions for that directory /path/to/?
    – Ian Turton
    Commented Jul 6, 2021 at 16:12
  • Yes, the package path is correct and permissions to the project folder are also set.
    – arjan
    Commented Jul 6, 2021 at 16:24
  • 1
    It would seem that each iteration of the for-loop opens the source data file without closing the file descriptor, based on the Creating pipes for GWakeup: Too many open files message. But if that is so, why is it opening an already opened file in the first place?
    – arjan
    Commented Jul 6, 2021 at 16:48
  • The GeoTools team is always happy to accept patches providing that everything still works on 8 & 11
    – Ian Turton
    Commented Jul 6, 2021 at 19:06


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.