私信  •  关注

user1942887

user1942887 最近创建的主题
user1942887 最近回复了
9 年前
回复了 user1942887 创建的主题 » 强制退出运行python中的线程[duplicate]

您可以在进程中执行命令,然后使用进程id终止它。 我需要在两个线程之间同步其中一个线程本身不返回。

processIds = []

def executeRecord(command):
    print(command)

    process = subprocess.Popen(command, stdout=subprocess.PIPE)
    processIds.append(process.pid)
    print(processIds[0])

    #Command that doesn't return by itself
    process.stdout.read().decode("utf-8")
    return;


def recordThread(command, timeOut):

    thread = Thread(target=executeRecord, args=(command,))
    thread.start()
    thread.join(timeOut)

    os.kill(processIds.pop(), signal.SIGINT)

    return;