LoginSignup
0
1

More than 5 years have passed since last update.

Python > Subprocess > サブスクリプトが終了時にメインスクリプトを終了する

Posted at
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
gnustep-gui-runtime v0.24.0-3.1
Jaybird RUN (ワイヤレスイヤホン)

プロジェクト開始: https://qiita.com/7of9/items/4077c92b9a773e054193

やりたいこと

subprocessを使った同期方法を検討した。

参考

code

main_upd_recv_180306.py
import time
import subprocess as sb

# on Python 3.5.2

'''
v0.1 Mar. 6, 2018
  - add main()
'''


def main():
    # run subprocess
    cmd = 'python3 sub_proc_180306.py'
    proc = sb.Popen(cmd.split(), shell=False)

    # main loop
    while True:
        time.sleep(1)  # sec
        print('hello')
        if proc.poll() is not None:
            break

main()

sub_proc_180306.py
import time

time.sleep(3)  # sec
print('finish')

実行

$ python3 main_upd_recv_180306.py 
hello
hello
hello
finish
hello

1秒程度の誤差はあるが、許容内である。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1