Difference between subprocess.Popen preexec_fn and start_new_session in python

后端 未结 1 1589
无人及你
无人及你 2021-01-04 18:03

What is the difference between these two options to start a new process with subprocess.Popen for python3.2+ under Linux:



        
相关标签:
1条回答
  • 2021-01-04 18:41

    According to the official Python Docs,

    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.

    If you need to modify the environment for the child use the env parameter rather than doing it in a preexec_fn. The start_new_session parameter can take the place of a previously common use of preexec_fn to call os.setsid() in the child.

    So I guess the answer to your question is that start_new_session was introduced to replace the common operation of using preexec_fn to set the session id through os.setsid(), which is not thread safe.

    0 讨论(0)
提交回复
热议问题