这是终止子进程的正确方法吗?

时间:2017-11-22 14:35:28

标签: python raspberry-pi subprocess record terminate

我使用arecord通过子进程来记录覆盆子pi:按下操纵杆按钮开始录制并再次按下按钮停止录制:我使用p.terminate()停止录制,这是我的代码。我不知道这是否是终止子流程的正确方法?

提前致谢。

if joystick.get_numbuttons() >= 1 and joystick.get_button( 0 ) == 0 and    button_pressed:
button_pressed = False
is_recording = not is_recording
print(is_recording)

#start recording
if is_recording:
    dotting = dotting + 1
    #recording code
   datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = "%s.wav" % datetime.now().strftime("%Y%m%d_%H%M%S")
    p=subprocess.Popen(['arecord', '--device=hw:1,0', '--format', 'S16_LE', '--rate', '44100-c1', filename], shell=False)

    #change background
    background = pygame.image.load(background_image2).convert()
    #start timer
    stopFlag = Event()
    timer = MyTimer(stopFlag,time.clock())
    #timer.start()
    text2 = font.render('.', True, BLACK)

    screen.blit(background, (0,0))

    stopFlag.set()

    #record, remaining here


#stop recording
else:
    #stop recording, save record
    timer.do_run = False
    backToPreviousQuestion()
    background = pygame.image.load(background_image).convert()
    p.terminate()

pygame.display.update()

1 个答案:

答案 0 :(得分:0)

一般来说,.terminate()是用于结束子流程的正确方法。您可以查看文档以获取更多信息:https://docs.python.org/3.3/library/