Django芹菜:函数在True While循环中只运行一次

2024-05-28 22:46:03 发布

您现在位置:Python中文网/ 问答频道 /正文

背景:我使用芹菜在django环境中运行python脚本,使用树莓pi从称重传感器测量重量。我正在量一瓶水,它把信息传给网络服务器。你知道吗

当在celery之外的python环境中运行时,下面的python代码运行得非常完美,但是当它由celery worker运行时,来自hx.get\重量(5) while循环中的函数在第一次迭代后不会更改。你知道吗

这个代码运行正常hx.get\重量(5) 当芹菜工人不运行时,每次(重量变化)返回不同的值。所以,我认为是芹菜引起了这个问题。你知道吗

import RPi.GPIO as GPIO
import time
import sys
from .hx711 import HX711, L287

app = Celery('robobud', broker='redis://localhost:6379/0')
log = logging.getLogger(__name__)

@app.task
async def pump(program_id, amount1, amount2):

    def cleanAndExit():
        print ("Cleaning...")
        GPIO.cleanup()
        print ("Bye!")
        sys.exit()

    ### Weight Class
    hx = HX711(5, 6 )
    hx.set_reading_format("LSB", "MSB")
    hx.set_reference_unit(112)
    hx.reset()
    hx.tare()



    ### Weight
    while True:
        try:

            val = hx.get_weight(5)
            print val

            hx.power_down()
            hx.power_up()
            time.sleep(1)

Tags: importappgetgpiotimedefsyscelery

热门问题