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

大家好,我不知道为什么唯一运行的块是我的第一个函数。


我试图将我的 coin_counter 最后一个值传递给第二个函数,但我的第一个函数在发布后没有传递该值。


而且它也不会打印到控制台


import RPi.GPIO as GPIO

import time

import threading


GPIO.setmode(GPIO.BCM)


GPIO.setup(27,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


lock = threading.Lock()


counter = 1

pulse = 0


def 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()


慕妹3146593
浏览 116回答 2
2回答

摇曳的蔷薇

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

相关分类

Python