Форум сайта python.su
Имеется макет программного продукта, главный сервис:
file subprocess_threadpool.py
import signal from subprocess import Popen, PIPE from time import sleep should_work = True def signal_handler(signum, frame): print 'Got signal %s' % (signum,) print frame global should_work should_work = False print 'Stopping' signal.signal(signal.SIGTERM, signal_handler) def run_as_subprocess(): subp = Popen(["python", "test_threadpool.py"], stdout=PIPE) output = subp.communicate()[0] #print output if __name__ == "__main__": while should_work: run_as_subprocess() sleep(5) print 'Parent app done'
import multiprocessing import subprocess from multiprocessing.pool import ThreadPool from time import sleep def do_calculation(data): print "calculating" subprocess.call(["logger", "%s - start" % data]) sleep(20) subprocess.call(["logger", "%s - finish" % data]) return data def test_apply_async(): inputs = list(range(10)) print 'Input :', inputs result = [] for item in inputs: result.append(pool.apply_async(do_calculation, args=(item,))) print 'Getting result' for res in result: data = res.get() print '\n---\n%s\n%s\n---\n' % (res, data) subprocess.call(["logger", "Got result: %s" % data]) if __name__ == '__main__': pool_size = 4 print pool_size pool = ThreadPool(processes=pool_size) test_apply_async() print "Job done"
python subprocess_threadpool.py
kill $parent_pid
kill -- -$parent_pid
Oct 28 00:06:11 xubuntu-laptop sergius: 1 - start Oct 28 00:06:11 xubuntu-laptop sergius: 0 - start Oct 28 00:06:11 xubuntu-laptop sergius: 3 - start Oct 28 00:06:11 xubuntu-laptop sergius: 2 - start
Отредактировано Master_Sergius (Окт. 28, 2015 11:20:58)
Офлайн
В общем, пришлось таки переделывать подход, проблема решена.
Офлайн