Timeout on subprocess readline in Python

后端 未结 6 2102
既然无缘
既然无缘 2020-12-12 22:18

I have a small issue that I\'m not quite sure how to solve. Here is a minimal example:

What I have

scan_process = subprocess.Popen(command, stdout=su         


        
6条回答
  •  自闭症患者
    2020-12-12 22:42

    Thanks for all the answers!

    I found a way to solve my problem by simply using select.poll to peek into standard output.

    import select
    ...
    scan_process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    poll_obj = select.poll()
    poll_obj.register(scan_process.stdout, select.POLLIN)
    while(some_criterium and not time_limit):
        poll_result = poll_obj.poll(0)
        if poll_result:
            line = scan_process.stdout.readline()
            some_criterium = do_something(line)
        update(time_limit)
    

提交回复
热议问题