为了账号安全,请及时绑定邮箱和手机立即绑定

只有第一个线程正在使用 python 线程运行

只有第一个线程正在使用 python 线程运行

慕妹3146593 2021-06-07 11:17:44
大家好,我不知道为什么唯一运行的块是我的第一个函数。我试图将我的 coin_counter 最后一个值传递给第二个函数,但我的第一个函数在发布后没有传递该值。而且它也不会打印到控制台import RPi.GPIO as GPIOimport timeimport threadingGPIO.setmode(GPIO.BCM)GPIO.setup(27,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)lock = threading.Lock()counter = 1pulse = 0def coin_counter():    global counter    lock.acquire()    try:        while True:            time.sleep(.1)            GPIO.wait_for_edge(27, GPIO.RISING)            #print("Pulse comming ! (%s)") %counter            counter += 1        return counter    finally:        lock.release()print(coin_counter())def get_pulse_count():    while True:        print('Hello World!')try:    coincounter = threading.Thread(target=coin_counter)    getpulse = threading.Thread(target=get_pulse_count)    coincounter.start()    getpulse.start()except KeyboardInterrupt:    coincounter.stop()    getpulse.stop()    GPIO.cleanup()
查看完整描述

2 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

我觉得问题符合print(coin_counter())。它应该被删除,因为我们在主线程(coin_counter()调用)中有无限循环。在此行之后不会执行任何代码。如果我们删除这一行并添加sleep进去,get_pulse_count()那么它就起作用了。此外return counter,如果你通过全局变量传递值不要求counter


查看完整回答
反对 回复 2021-06-09
  • 2 回答
  • 0 关注
  • 115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号