简体   繁体   English

Python subprocess.call/Popen/系统问题

[英]Python subprocess.call/Popen/system issue

I am starting a subprocess by different ways - subprocess.call, Popen, os.system. 我正在通过不同的方式启动子流程-subprocess.call,Popen,os.system。

For example: 例如:

subprocess.call('wine application.exe', shell=True)

But python starts 2 processes: 但是python启动2个进程:

/bin/sh -c wine application.exe application.exe

When I am starting this application manually by calling in console window wine application.exe I see only application.exe instance in ps ax . 当我通过在控制台窗口wine application.exe调用手动启动此应用application.exeps ax仅看到application.exe实例。

Is it normal? 正常吗 My guess is that is ok because it is subprocess and it's parent waits for child finish. 我的猜测是可以的,因为它是子进程,并且父级等待子级完成。 BUT, why this behavior not the same as I'd launched application from console? 但是,为什么这种行为与我从控制台启动应用程序不同? Where in this case parent hides? 在这种情况下,父母隐藏在哪里?

Update: I've thought hard and understood that this is really parent process. 更新:我一直在努力思考,并了解这确实是父流程。 If I will launch application from python with: subprocess.call('wine application.exe &', shell=True) it will have same behavior as in shell(because shell is true ). 如果我使用以下命令从python启动应用程序: subprocess.call('wine application.exe &', shell=True) ,它将具有与shell中相同的行为(因为shelltrue )。 So in this case it will be only one instance - the one you called by this subprocess call. 因此,在这种情况下,它将只是一个实例-该子流程调用所调用的实例。

But anyway I still don't understand why there is 2 processes instead of simply one. 但是无论如何,我仍然不明白为什么会有2个过程而不是一个。 Invoker - okay, but where second process came from? 调用者-好的,但是第二个步骤来自哪里? Why it doesn't work with 1 process? 为什么它不适用于1个进程?

When you start an application with shell=True, python starts a shell and the shell starts the program. 当您使用shell = True启动应用程序时,python将启动一个shell,然后shell将启动该程序。 That's why you see two programs. 这就是为什么您看到两个程序。 If you run it like 如果你像这样运行

subprocess.call(['wine', 'application.exe'])

you will only see one. 您只会看到一个。

When you ran it from the command line, you were already in the shell so you only saw 1. 当您从命令行运行它时,您已经在shell中,因此您只看到1。

Update 更新

When you ran subprocess.call('wine application.exe &', shell=True) , python executed a shell which executed wine. 当您运行subprocess.call('wine application.exe &', shell=True) ,python执行了执行wine的shell。 Since you added &, the shell backgrounded wine and exited. 既然添加了&,该外壳就会使酒背景化并退出。 While this may be what you want, the potential downsides are that python doesn't wait for the process to complete and there will be a zombie process left over when your program exits. 尽管这可能就是您想要的,但潜在的缺点是python不等待进程完成,并且在程序退出时会留下一个僵尸进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM