Version 3.6.1 ON CentOS7
スレッドを立ててプロセスの状態を表示するプログラム
import logging
import traceback
import subprocess
import threading
import time
logging.basicConfig(level=logging.DEBUG, format='(%(threadName)s) %(message)s')
def print_ps():
for i in range(10):
logging.debug(i)
try:
cmd = ['ps', '-ef']
buf = subprocess.check_call(cmd)
print(buf)
except:
logging.debug(traceback.format_exc())
time.sleep(1)
# End of for
#
# main
#
logging.debug("Start");
thread = threading.Thread(target=print_ps)
thread.start()
# スレッド終了待ち
thread.join()
logging.debug("End");
実行結果(MainThread) Start (Thread-1) 0 UID PID PPID C STIME TTY TIME CMD ... (Thread-1) 1 UID PID PPID C STIME TTY TIME CMD ... (Thread-1) 2 UID PID PPID C STIME TTY TIME CMD ... (Thread-1) 3 UID PID PPID C STIME TTY TIME CMD ... (Thread-1) 4 UID PID PPID C STIME TTY TIME CMD ... (Thread-1) 5 UID PID PPID C STIME TTY TIME CMD ... (Thread-1) 6 UID PID PPID C STIME TTY TIME CMD ... (Thread-1) 7 UID PID PPID C STIME TTY TIME CMD ... (Thread-1) 8 UID PID PPID C STIME TTY TIME CMD ... (Thread-1) 9 UID PID PPID C STIME TTY TIME CMD ... (MainThread) End