PY3.W1509

Subprocess popen preexec-fn

The preexec_fn parameter is not safe to use in the presence of threads in your application. The child process could deadlock before exec is called. If you must use it, keep it trivial! Minimize the number of libraries you call into.

Noncompliant Code:

Copy
import subprocess

proc = subprocess.Popen(
    ['ls', '/home'],
    preexec_fn=lambda: print('Some actions'),
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

Compliant Code:

Copy
import subprocess

proc = subprocess.Popen(
    ['script.sh'],  # a path to a script that will do a pre command
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

The content on this page is adapted from the Pylint User Guide, Copyright ©2003-2022, Logilab, PyCQA and contributors. All rights reserved. https://pylint.pycqa.org/en/latest/index.html#, and is used under the Python Software Foundation License Version 2. Examples, recipes, and other code in the Pylint documentation are additionally licensed under the Zero Clause BSD License.