본문으로 바로가기
반응형

Difference between Subprocess-popen and os-system


장고로 만든 카카오톡 학식봇 코드를 수정하다가

특정한 조건에 걸리면 파이썬 스크립트를 실행하게 만들고 있었다.

os.system("python3 a.py") 형식으로 실행을 했었는데

장고 실행화면에 그대로 노출이 될 뿐더러 request timeout이 걸려버려서

제대로 봇에 먹히지 않았다.

그래서 이유를 찾아보니 다음과 같았다.


interface. Those were designed similar to the Unix Popen command.

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed

where as

The popen() function opens a process by creating a pipe, forking, and invoking the shell.

If you are thinking, which one to use, then use subprocess definitely because you you have all facilities for execution, plus additional control over the process.


쉽게 말해서 system은 현재의 프로세스에서 작업 후 해당 명령어가 완료 될 때 까지 기다린다.

하지만 popen은 fork처럼 하나의 새로운 프로세스를 생성하고 작업을 하기 때문에 기존 프로세스에 영향을 끼치지 않는다.

반응형