for line in p.stdout:
    logfile.write(line)
if re.search('done', line):
    break
print('waiting')
os.kill(p.pid, signal.CTRL_C_EVENT)
Simulate Ctrl-C keyboard interrupt in Python while working in Linux