我对编码和其他东西很陌生。我正在使用一个带有HX711转接板的数字称重秤,并通过4位7段显示器输出数值。
weighing()
循环读取值的速率比我的显示器多路复用时间慢,所以代码不会继续,直到读取了值,导致显示器像地狱一样闪烁。因此,我尝试通过concurrent.futures
同时运行weighing()
循环和displaying()
循环。但是代码只执行weighing()
一次,然后被困在display()
循环中,所以它们不是并发运行的?
我的代码一定有问题,请帮助我澄清并为其他方法留下任何建议
import time
import RPi.GPIO as GPIO
import concurrent.futures
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
segments = [2, 3, 4, 5, 6, 7, 8, 9]
digits = [12, 13, 18, 19]
GPIO.setup(segments, GPIO.OUT)
GPIO.output(segments, GPIO.HIGH)
GPIO.setup(digits, GPIO.OUT)
GPIO.output(digits, GPIO.LOW)
nums ={
0:(1,1,1,1,1,1,0),
1:(0,1,1,0,0,0,0),
2:(1,1,0,1,1,0,1),
3:(1,1,1,1,0,0,1),
4:(0,1,1,0,0,1,1),
5:(1,0,1,1,0,1,1),
6:(1,0,1,1,1,1,1),
7:(1,1,1,0,0,0,0),
8:(1,1,1,1,1,1,1),
9:(1,1,1,1,0,1,1)}
switchpolarity = {1: 0,
0:1}
def display(value):
while 1:
s = [int(d) for d in str(value)]
for digit in range(0,len(s)):
for segment in range(0,7):
GPIO.output(segments[segment], switchpolarity[nums[s[digit]][segment]])
GPIO.output(digits[digit], 1)
time.sleep(0.01)
GPIO.output(digits[digit], 0)
EMULATE_HX711=False
if not EMULATE_HX711:
from hx711 import HX711
else:
from emulated_hx711 import HX711
def weighing():
while 1:
val = round(abs(hx.get_weight(1)))
print(val)
hx.power_down()
hx.power_up()
return(val)
hx = HX711(9, 10)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(754)
hx.reset()
hx.tare()
print("Tare done! Add weight now...")
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
weighing = executor.submit(weighing)
displaying = executor.submit(display, (t1.result()))
except(KeyboardInterrupt):
GPIO.cleanup()
我对代码中的拼写错误感到非常抱歉,因为我在发布时未经测试就更改了进程名称。这是我的新代码,我可以说没有愚蠢的错误:
import time
import RPi.GPIO as GPIO
import concurrent.futures
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
segments = [2, 3, 4, 5, 6, 7, 8, 9]
digits = [12, 13, 18, 19]
GPIO.setup(segments, GPIO.OUT)
GPIO.output(segments, GPIO.HIGH)
GPIO.setup(digits, GPIO.OUT)
GPIO.output(digits, GPIO.LOW)
nums ={
0:(1,1,1,1,1,1,0),
1:(0,1,1,0,0,0,0),
2:(1,1,0,1,1,0,1),
3:(1,1,1,1,0,0,1),
4:(0,1,1,0,0,1,1),
5:(1,0,1,1,0,1,1),
6:(1,0,1,1,1,1,1),
7:(1,1,1,0,0,0,0),
8:(1,1,1,1,1,1,1),
9:(1,1,1,1,0,1,1)}
switchpolarity = {1: 0,
0:1}
def display(value):
while 1:
s = [int(d) for d in str(value)]
for digit in range(0,len(s)):
for segment in range(0,7):
GPIO.output(segments[segment], switchpolarity[nums[s[digit]][segment]])
GPIO.output(digits[digit], 1)
time.sleep(0.01)
GPIO.output(digits[digit], 0)
EMULATE_HX711=False
if not EMULATE_HX711:
from hx711 import HX711
else:
from emulated_hx711 import HX711
def weighing():
while 1:
val = round(abs(hx.get_weight(1)))
print(val)
hx.power_down()
hx.power_up()
return(val)
hx = HX711(9, 10)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(754)
hx.reset()
hx.tare()
print("Tare done! Add weight now...")
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
weighing1 = executor.submit(weighing)
displaying1 = executor.submit(display, (weighing1.result()))
except(KeyboardInterrupt):
GPIO.cleanup()
这是"称重"线程的主要功能:
def weighing():
while 1:
val = round(abs(hx.get_weight(1)))
...
return(val)
return(val)
语句将导致函数在循环的第一次迭代结束时返回。一旦函数返回,线程就结束了。它再也不会运行了。
这就是你启动线程的方式:
weighing = executor.submit(weighing)
displaying = executor.submit(display, (t1.result()))
如果我理解正确,*executor.submit(weighing)
调用返回未来,t1.result()
**等待未来的完成,然后返回weighing
函数返回的任何值。
这意味着,executor.submit(display, ...)
将不会发生,直到t1.result()
返回一个值,这意味着第二个线程甚至不能启动,直到第一个线程完成。
IMO;
weighing()
函数应该更新一个全局变量并永远继续循环,而不是返回一个值,并且display()
函数应该通过从全局变量和中复制来获得它显示的值您可以忽略
ProcessPoolExecutor
返回的期货。您并没有真正将其用作执行器服务:您只是将其用作创建两个线程的一种方式。
*我实际上不是Python大师。
**我假设t1.result()
是一个复制/粘贴错误,您的意思是说weighing.result()
。