【问题标题】:Multiprocessing two functions that both use time.sleep()多处理两个都使用 time.sleep() 的函数
【发布时间】:2015-11-29 01:21:49
【问题描述】:

我正在使用 raspberry pi 开展一个项目,我需要让两个函数并行运行,这两个函数都需要访问相同的 GPIO 引脚(它们需要打开/关闭相同的泵)。

问题是功能 1 需要每 40 分钟访问一次这些泵并一次使用它们 5 分钟,而功能 2 需要每 3 小时访问一次它们一次 5 分钟。我保持泵处于活动状态的方法是打开 GPIO 引脚,使用 time.sleep(),然后将其关闭。在函数使用泵后,他们需要将化学物质分配到水中并等待这些化学物质溶解(函数 1 等待 30 分钟,函数 2 等待 3 小时)。

我正在寻找并行运行这些函数的最佳方式,同时考虑到它们之间可能存在的调度/时序冲突。我希望功能 1 能够使用泵,即使功能 2 正在等待其化学物质溶解。我正在考虑使用全局变量来检查泵是否正在使用,以让函数知道他们需要等待访问这些泵,但经过一些测试,我不确定这是否适用于多处理。

我设置了一些测试代码来模拟功能的时序。根据我的输出,函数 1 和函数 2 似乎都在同时分配它们的化学品。非常感谢任何想法或建议。

import time
from multiprocessing import Process

pumpInUse = False #used to store the state of the pumps

def function1():
    global pumpInUse
    if pumpInUse is False:
        print "starting function1 test @ " + str((time.strftime("%H:%M:%S")))
        pumpInUse = True #turn pump on
        time.sleep(5)  #simulating 5 minutes of pump use
        print "function1 test complete @ " + str((time.strftime("%H:%M:%S")))
        pumpInUse = False #turn pump off
        function1status = 'bad' #simulating bad chemical level

        if function1status == 'bad':
            print "dispense chemicals @ " + str((time.strftime("%H:%M:%S")))
            time.sleep(10) #simulate wait 30 minutes after chemcials dispensed
            print "checking water @ " + str((time.strftime("%H:%M:%S")))
            pumpInUse = True
            time.sleep(5) #simulating 5 minutes of pump use
            print "function1 complete @ " + str((time.strftime("%H:%M:%S")))
            pumpInUse = False

def function2():
    global pumpInUse
    if pumpInUse is False:
        print "starting function2 test @ " + str((time.strftime("%H:%M:%S")))
        pumpInUse = True
        time.sleep(5) #simulating 5 minutes of pump use
        print "function2 test complete @ " + str((time.strftime("%H:%M:%S")))
        pumpInUse = False
        function2status = 'bad' #simulating bad chemical level

        if function2status == 'bad':
            print "dispense chemicals @ " + str((time.strftime("%H:%M:%S")))
            time.sleep(30) #simulate wait 3 hours after chemicasl dispensed
            print "checking water @ " + str((time.strftime("%H:%M:%S")))
            pumpInUse = True
            time.sleep(5) #simulating 5 minutes of pump use
            print "function2  complete @ " + str((time.strftime("%H:%M:%S")))
            pumpInUse = False

if __name__ == '__main__':
    #while True:
        p1 = Process(target=function1)
        p2 = Process(target=function2)
        p1.start()
        p2.start()
        p1.join()
        p2.join()

【问题讨论】:

  • 使用这样的变量在线程之间进行通信可能是非常危险的。您应该考虑使用适当的线程安全方式在线程之间进行通信:docs.python.org/3/library/asyncio-sync.html。在您的情况下,“锁”可能很有用。

标签: python multithreading raspberry-pi multiprocessing python-multiprocessing


【解决方案1】:

您正在寻找线程(或进程)synchronization primitives,在这种情况下,可能是Lockfunction1 将获得一个阻塞锁并在完成后释放它。当function2试图获取锁时,它会自动等待直到锁被释放。

【讨论】:

  • 是的!使用锁正是我所需要的。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多