subprocess popen 子程序退出的問題
最近在專案中遇到一個需求,前端發來一個命令,這個命令是去執行傳遞過來的一個指令碼(shell 或者python),並返回指令碼的標準輸出和標準出錯,如果執行超過設定時間還沒結束就超時,然後終止指令碼的執行。實現這個功能,自然而然先想到的是subprocess這個庫了。
因此,在後端的一個指令碼中呼叫python的subprocess去執行傳遞過來的指令碼,通常情況下subprocess都能執行的很好,完成指令碼的執行並返回。最初的實現程式碼如下:
run_cmd.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
from threading import Timer
import os
class test(object):
def __init__(self):
self.stdout = []
self.stderr = []
self.timeout = 10
self.is_timeout = False
pass
def timeout_callback(self, p):
print 'exe time out call back'
print p.pid
try:
p.kill()
except Exception as error:
print error
def run(self):
cmd = ['bash', '/home/XXXX/test.sh']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
my_timer = Timer(self.timeout, self.timeout_callback, [p])
my_timer.start()
try:
print "start to count timeout; timeout set to be %d \n" % (self.timeout,)
stdout, stderr = p.communicate()
exit_code = p.returncode
print exit_code
print type(stdout), type(stderr)
print stdout
print stderr
finally:
my_timer.cancel()
但是偶然間測試一個shell指令碼,這個shell指令碼中有一行ping www.baidu.com &,shell指令碼如下:
test.sh
#!/bin/bash
ping www.baidu.com (&) #加不加&都沒區別
echo $$
python(父程序)用subprocess.Popen新建一個程序(子程序)去開啟一個shell, shell新開一個子程序(孫程序)去執行ping www.baidu.com的命令。由於孫程序ping www.baidu.com一直在執行,就類似於一個daemon程式,一直在執行。在超時時間後,父程序殺掉了shell子程序,但是父程序阻塞在了p.communicate函數了,是阻塞在了呼叫wait()函式之前,感興趣的朋友可以看一下原始碼_communicate函式,linux系統重點看_communicate_with_poll和_communicate_with_select函式,你會發現是阻塞在了while迴圈裡面,因為父程序一直在獲取輸出,而孫程序一直像一個daemon程式一樣,一直在往子程序的輸出寫東西,而子程序的檔案控制代碼繼承自父程序。雖然shell子程序被殺掉了,但是父程序裡面的邏輯並沒有因為子程序被意外的幹掉而終止,(因為孫程序一直有輸出到子程序的stdout,導致子程序的stdout一直有輸出,也就是父程序的stdout也有輸出),所以while迴圈一直成立,就導致了阻塞,進而導致wait()沒有被呼叫,所以子程序沒有被回收,就成了殭屍程序。
要完美的解決這個問題就是即要能獲取到subprocess.Popen的程序的輸出,在超時又要能殺掉子程序,讓主程序不被阻塞。
一開始比較急,也對subprocess.Popen沒有深入的去用過,嘗試了一個low B的辦法,就是不用subprocess.Popen.communicate()去獲取輸出,而是直接去讀檔案,然後超時後不去讀檔案。程式碼如下:
run_cmd.py第一個改版
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
from threading import Timer
import os
class test(object):
def __init__(self):
self.stdout = []
self.stderr = []
self.timeout = 10
self.is_timeout = False
pass
def timeout_callback(self, p):
self.is_timeout = True
print "time out"
def run(self):
cmd = ['bash', '/home/zhangxin/work/baofabu/while_test.sh']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
my_timer = Timer(self.timeout, self.timeout_callback, [p])
my_timer.start()
try:
print "start to count timeout; timeout set to be %d \n" % (self.timeout,)
for line in iter(p.stdout.readline, b''):
print line
if self.is_timeout:
break
for line in iter(p.stderr.readline, b''):
print line
if self.is_timeout:
break
finally:
my_timer.cancel()
p.stdout.close()
p.stderr.close()
p.kill()
p.wait()
這樣雖然能獲取輸出,在超時後也不再阻塞,寫完過後返回來再看時發現,其實在最開始的那一版程式碼中,只要在超時的回撥函式中加上p.stdout.close()和p.stderr.clode(), p.communicate就不再阻塞了,其實問題也就解決了。 但是還會存在一個潛在的問題,父程序結束了,沒有其他程序去讀取PIPE,daemon孫程序一直往PIPE寫,最後導致PIPE填滿,孫程序也被阻塞。
所以這樣處理其實沒任何意義,因為孫程序沒有被終止掉,只是簡單的關閉了管道。 所以在假期,我仔細的在網上找了找,看了看subprocess,發現subprocess.Popen有一個引數preexec_fn,呼叫subprocess.Popen時傳遞preexec_fn=os.setsid或者preexec_fn=os.setpgrp,然後在超時的時候執行os.killpg(p.pid, signal.SIGKILL)就可以殺掉子程序以及在同一個會話的所有程序。所以將run函式的subprocess.Popen改為
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid)
同時將timeout_callback函式改成如下就可以了:
def timeout_callback(self, p):
self.is_timeout = True
print 'exe time out call back'
print p.pid
try:
os.killpg(p.pid, signal.SIGKILL)
except Exception as error:
print error
關於preexec_fn=os.setsid的作用,以下摘自https://blog.tonyseek.com/post/kill-the-descendants-of-subprocess/
執行程式 fork 以後,產生的子程序都享有獨立的程序空間和 pid,也就是它超出了我們觸碰的範圍。好在 subprocess.Popen 有個 preexec_fn 引數,它接受一個回撥函式,並在 fork 之後 exec 之前的間隙中執行它。我們可以利用這個特性對被執行的子程序做出一些修改,比如執行 setsid() 成立一個獨立的程序組。
Linux 的程序組是一個程序的集合,任何程序用系統呼叫 setsid 可以建立一個新的程序組,並讓自己成為首領程序。首領程序的子子孫孫只要沒有再呼叫 setsid 成立自己的獨立程序組,那麼它都將成為這個程序組的成員。 之後程序組內只要還有一個存活的程序,那麼這個程序組就還是存在的,即使首領程序已經死亡也不例外。 而這個存在的意義在於,我們只要知道了首領程序的 pid (同時也是程序組的 pgid), 那麼可以給整個程序組傳送 signal,組內的所有程序都會收到。
因此利用這個特性,就可以通過 preexec_fn 引數讓 Popen 成立自己的程序組, 然後再向程序組傳送 SIGTERM 或 SIGKILL,中止 subprocess.Popen 所啟動程序的子子孫孫。當然,前提是這些子子孫孫中沒有程序再呼叫 setsid 分裂自立門戶。
至於setsid和setpgrp有什麼區別,看了各自的man page,還不是很明白,如果有大兄弟知道,並且不吝留言分享告知,感激涕零!
subprocess.Popen只能執行命令或者指令碼,而不能像threading的thread庫一樣執行函式,那麼如何在一個只有.py檔案的情況下像thread一樣執行subprocess.Popen呢? 在呼叫subprocess.Popen的py我們可以把要執行的指令碼內容寫到一個臨時檔案,也即是類似於thread的target函式,然後用subprocess.Popen執行這個臨時指令碼,這樣就可以不用預先存在多個指令碼。。如下面的例子:
import os
import signal
import subprocess
import tempfile
import time
import sys
def show_setting_prgrp():
print('Calling os.setpgrp() from {}'.format(os.getpid()))
os.setpgrp()
print('Process group is now {}'.format(
os.getpid(), os.getpgrp()))
sys.stdout.flush()
# 這次的重點關注是這裡
script = '''#!/bin/sh
echo "Shell script in process $$"
set -x
python3 signal_child.py
'''
script_file = tempfile.NamedTemporaryFile('wt')
script_file.write(script)
script_file.flush()
proc = subprocess.Popen(
['sh', script_file.name],
preexec_fn=show_setting_prgrp,
)
print('PARENT : Pausing before signaling {}...'.format(
proc.pid))
sys.stdout.flush()
time.sleep(1)
print('PARENT : Signaling process group {}'.format(
proc.pid))
sys.stdout.flush()
os.killpg(proc.pid, signal.SIGUSR1)
time.sleep(3)
當然也可以在shell腳本里面用exec來執行命令,那麼就只有父程序和子程序,沒有孫程序的概念了。
其實關於阻塞問題,也可以將subprocess.Popen的輸出重定向到檔案。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
from threading import Timer
import os
import time
import signal
class test(object):
def __init__(self):
self.stdout = []
self.stderr = []
self.timeout = 6
self.is_timeout = False
pass
def timeout_callback(self, p):
print 'exe time out call back'
try:
p.kill()
# os.killpg(p.pid, signal.SIGKILL)
except Exception as error:
print error
def run(self):
stdout = open('/tmp/subprocess_stdout', 'wb')
stderr = open('/tmp/subprocess_stderr', 'wb')
cmd = ['bash', '/home/xxx/while_test.sh']
p = subprocess.Popen(cmd, stdout=stdout.fileno(), stderr=stderr.fileno())
my_timer = Timer(self.timeout, self.timeout_callback, [p])
my_timer.start()
print p.pid
try:
print "start to count timeout; timeout set to be %d \n" % (self.timeout,)
p.wait()
finally:
my_timer.cancel()
stdout.flush()
stderr.flush()
stdout.close()
stderr.close()
寫在最後,關於p = subprocess.Popen,最好用p.communicate.而不是直接用p.wait(), 因為p.wait()有可能因為子程序往PIPE寫的時候寫滿了,但是子程序還沒有結束,導致子程序阻塞,而父程序一直在wait(),導致父程序阻塞。而且p.wait()和p.communicate不能一起用,因為p.communicate裡面也會去呼叫wait()。
在linux平臺下,p.wait()其實最後呼叫的是os.waitpid(), 我們自己用的時候,也儘量用waitpid,而不是wait(),因為多次呼叫waitpid去wait同一個程序不會導致阻塞,但是程式中多次呼叫wait就很有可能會被阻塞,詳見wait函式的作用。
其實阻塞的根本原因還是因為PIPE滿了,所以用PIPE的時候,最好和select或者poll模型一起使用,防止讀、寫阻塞。 PIPE管道是系統呼叫,os.pipe產生的一個檔案,只不過他有兩個fd,一個用於讀,一個用於寫,當讀寫端都被關閉後,核心會自動回收。你可以理解核心在記憶體中開闢了一個佇列,一端讀,一端寫。
管道在程序間通訊(IPC)使用很廣泛,shell命令就使用的很廣泛。比如:
ps –aux | grep mysqld
上述命令表示獲取mysqld程序相關的資訊。這裡ps和grep兩個命令通訊就採用了管道。管道有幾個特點:
-
管道是半雙工的,資料只能單向流動,ps命令的輸出是grep的輸出
-
只能用於父子程序或兄弟程序通訊,這裡可以認為ps和grep命令都是shell(bash/pdksh/ash/dash)命令的子程序,兩者是兄弟關係。
-
管道相對於管道兩端的程序而言就是一個檔案,並且只存在於記憶體中。
-
到這裡大家可能會有一個疑問,管道兩端的程序,寫入程序不斷的寫,讀取程序不斷的讀,那麼什麼時候結束呢?比如我們剛剛這個命令很快就結束了,它的原理是怎麼樣的呢?對於管道,這裡有兩個基本原則:寫入端不斷往管道寫,並且每次寫到管道末尾;讀取端則不斷從管道讀,每次從頭部讀取。
1.當讀一個寫端已經關閉的管道時,在所有資料被讀取後,read返回0,以指示達到檔案結束處。
2.當寫一個讀端已經關閉的管道時,會產生sigpipe資訊。
結合這個例子,當ps寫管道結束後,就會自動關閉,此時grep程序read就會返回0,然後自動結束。
具體pipe可以參見http://man7.org/linux/man-pages/man7/pipe.7.html
最近有發現了一個有趣的shell命令timeout,結合python 2.7的subprocess.Popen(python3的subprocess.Popen自帶timeout引數),可以做到超時後終止程序。
cmd = ['timeout', 'bash', 'xxxxx']
subprocess.Popen(cmd)