首页 > 解决方案 > 为什么“if keyboard.is_pressed('q'):”不在我的代码中执行?

问题描述

在我的代码中,在 f() 中,“kbd.is_pressed('q'):”不起作用。当按下“q”时,它不会打印消息并且循环不会结束。

from multiprocessing import Process, Pipe
import multiprocessing as mp
import keyboard as kbd

x = 0
frames = []

def f(conn):
    global x
    while x == 0:
        frames.append(1)
        conn.send([42, None, 'hello', frames])
        if kbd.is_pressed('q'):
            print("Q was pressed.")
            break
    conn.close()
    print("Pipe is closed!")

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print(mp.current_process)
    print(parent_conn.recv())   # prints "[42, None, 'hello']"
    f(child_conn)
    print(str(frames))
    p.join()
    print("CPU's = " + str(mp.cpu_count()))
    p.terminate()
    print(mp.parent_process)

标签: pythonmultiprocessingkeyboard-events

解决方案


推荐阅读