这是我第一次做Python程序,我做了一个简单的Python程序,如下所示,但它在键盘点处冻结.is_Press()调用,它无法打印我的测试消息"Loop 1 time now",这意味着按下任何键都不会给出任何响应,这个函数调用根本不会返回,甚至Ctrl-C都不能中断程序并返回命令提示符控制.
import threading
import asyncio
import time
import keyboard
flagkeypress = False
keycode = 'a'
flagserverconnected = False
def task1():
print("Connecting to server")
asyncio.run(connect2svr())
print("task1 ended")
async def connect2svr():
global flagkeypress
global keycode
global flagserverconnected
try:
print("Connected to server")
flagserverconnected = True
keeploopBLE = True
while keeploopBLE:
if flagkeypress == True:
flagkeypress = False
if keycode == 'q':
for k in range(14, 19):
sk="##SP"+ f"{k:02}"
print(sk)
await asyncio.sleep(1)
if keycode == ']':
keeploopBLE = False
except Exception as e:
print(f"Error: {e}")
print("Start of my main program")
# Create threads
t1 = threading.Thread(target=task1)
# Start threads
t1.start()
keeploop = True
while keeploop:
print("\nOptions: [Q]=Get Version, [T]=Get Time, [P]=Start/End Get GPS Time\n")
print("Press key for command:")
waitkey = True
while waitkey:
if keyboard.is_pressed('t'):
my_kevent = keyboard.read_event()
keyn = my_kevent.name
print(f"key {keyn} is pressed, program going to end")
keycode = ']'
flagkeypress = True
waitkey = False
keeploop = False
elif keyboard.is_pressed('q'):
my_kevent = keyboard.read_event()
keyn = my_kevent.name
print(f"key {keyn} is pressed")
keycode = 'q'
flagkeypress = True
waitkey = False
print("loop 1 time now")
time.sleep(0.2)
time.sleep(0.5)
print("Main program ended")
t1.join()
如果我删除了task1线程,则Python代码简化如下,然后它就会像我预期的那样工作,"loop 1 time now"被连续打印出来.线程task1的存在影响键盘模块API不工作的根本原因是什么?但到目前为止,我不能100%地说冻结是由线程task1的存在引起的,因为我在task1存在之前有另一个版本,如果我删除一个Waiting While循环,它仍然可以工作,但在我进一步将其简化为这两个版本之前,我没有保存那个版本.
import asyncio
import time
import keyboard
flagkeypress = False
keycode = 'a'
print("Start of my main program")
keeploop = True
while keeploop:
print("\nOptions: [Q]=Get Version, [T]=Get Time, [P]=Start/End Get GPS Time\n")
print("Press key for command:")
waitkey = True
while waitkey:
if keyboard.is_pressed('t'):
my_kevent = keyboard.read_event()
keyn = my_kevent.name
print(f"key {keyn} is pressed, program going to end")
keycode = ']'
flagkeypress = True
waitkey = False
keeploop = False
elif keyboard.is_pressed('q'):
my_kevent = keyboard.read_event()
keyn = my_kevent.name
print(f"key {keyn} is pressed")
keycode = 'q'
flagkeypress = True
waitkey = False
print("loop 1 time now")
time.sleep(0.2)
time.sleep(0.5)
print("Main program ended")
输出:
C:\Projects\pytest1>python pytest3.py
Start of my main program
Options: [Q]=Get Version, [T]=Get Time, [P]=Start/End Get GPS Time
Press key for command:
loop 1 time now
loop 1 time now
loop 1 time now
loop 1 time now
key q is pressed
loop 1 time now
Options: [Q]=Get Version, [T]=Get Time, [P]=Start/End Get GPS Time
Press key for command:
loop 1 time now
loop 1 time now
loop 1 time now
loop 1 time now
loop 1 time now
key q is pressed
loop 1 time now
Options: [Q]=Get Version, [T]=Get Time, [P]=Start/End Get GPS Time
Press key for command:
loop 1 time now
loop 1 time now
key q is pressed
loop 1 time now
Options: [Q]=Get Version, [T]=Get Time, [P]=Start/End Get GPS Time
Press key for command:
loop 1 time now
loop 1 time now
key q is pressed
loop 1 time now
Options: [Q]=Get Version, [T]=Get Time, [P]=Start/End Get GPS Time
Press key for command:
loop 1 time now
loop 1 time now
loop 1 time now
key q is pressed
loop 1 time now
Options: [Q]=Get Version, [T]=Get Time, [P]=Start/End Get GPS Time
Press key for command:
loop 1 time now
loop 1 time now
loop 1 time now
loop 1 time now
loop 1 time now
key q is pressed
loop 1 time now
Options: [Q]=Get Version, [T]=Get Time, [P]=Start/End Get GPS Time
Press key for command:
loop 1 time now
loop 1 time now
loop 1 time now
loop 1 time now
loop 1 time now
key t is pressed, program going to end
loop 1 time now
Main program ended