只有第一个线程是 运行 使用 python 线程
Only first thread is running using python threading
大家好,我不知道为什么唯一运行的块是我的第一个函数。
我正在尝试将我的 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()
我觉得问题符合print(coin_counter())
。它应该被删除,因为我们在主线程中有无限循环(coin_counter()
调用)。此行之后不会执行任何代码。如果我们删除这一行并将 sleep
添加到 get_pulse_count()
中,那么它就可以工作了。如果通过全局变量 counter
.
传递值,也不需要 return 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
global pulse
lock.acquire()
try:
time.sleep(.1)
GPIO.wait_for_edge(27, GPIO.RISING)
print("Pulse comming ! ", counter)
counter += 1
pulse = counter
finally:
lock.release()
def get_pulse_count():
global pulse
lock.acquire()
try:
print(pulse)
finally:
lock.release()
while True:
time.sleep(.1)
try:
coincounter = threading.Thread(target=coin_counter)
coincounter.start()
getpulse = threading.Thread(target=get_pulse_count)
getpulse.start()
except KeyboardInterrupt:
coincounter.stop()
getpulse.stop()
GPIO.cleanup()
大家好,我不知道为什么唯一运行的块是我的第一个函数。
我正在尝试将我的 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()
我觉得问题符合print(coin_counter())
。它应该被删除,因为我们在主线程中有无限循环(coin_counter()
调用)。此行之后不会执行任何代码。如果我们删除这一行并将 sleep
添加到 get_pulse_count()
中,那么它就可以工作了。如果通过全局变量 counter
.
return 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
global pulse
lock.acquire()
try:
time.sleep(.1)
GPIO.wait_for_edge(27, GPIO.RISING)
print("Pulse comming ! ", counter)
counter += 1
pulse = counter
finally:
lock.release()
def get_pulse_count():
global pulse
lock.acquire()
try:
print(pulse)
finally:
lock.release()
while True:
time.sleep(.1)
try:
coincounter = threading.Thread(target=coin_counter)
coincounter.start()
getpulse = threading.Thread(target=get_pulse_count)
getpulse.start()
except KeyboardInterrupt:
coincounter.stop()
getpulse.stop()
GPIO.cleanup()