On 08/11/2019 17:05, Roman Kagan wrote: > On Fri, Nov 08, 2019 at 01:49:50PM +0000, Vladimir Sementsov-Ogievskiy wrote: >> 01.11.2019 19:54, Andrey Shinkevich wrote: >>> +def check_proc_NBD(proc, connector): >>> + try: >>> + exitcode = proc.wait(timeout=10) >>> + >>> + if exitcode < 0: >>> + log('NBD {}: EXIT SIGNAL {}\n'.format(connector, -exitcode)) >>> + log(proc.communicate()[0]) >>> + else: >>> + line = proc.stdout.readline() >> >> >> could we use proc.communicate() for both cases, what is the difference? > > In fact if proc produces any non-trivial amount of output you are better > off using .communicate() otherwise your child may block on output and > never exit. See > https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate > for how to express the above logic correctly. The exit code *after* > .communicate is available in .returncode. >
The pattern by the link above does not work (Python3): proc = subprocess.Popen(...) try: outs, errs = proc.communicate(timeout=15) except TimeoutExpired: proc.kill() outs, errs = proc.communicate() as 'proc' cannot be used for output after being killed. It results in another exception being raised. Andrey >> >>> + log('NBD {}: {}'.format(connector, line.rstrip())) >>> + >>> + except subprocess.TimeoutExpired: >>> + proc.kill() >>> + log('NBD {}: ERROR timeout expired'.format(connector)) >>> + finally: >>> + if connector == 'server': >>> + os.remove(nbd_sock) >>> + os.remove(conf_file) > > Roman. > -- With the best regards, Andrey Shinkevich