简体   繁体   English

多处理两个都使用time.sleep()的函数

[英]Multiprocessing two functions that both use time.sleep()

I'm working on a project with the raspberry pi where I need to have two functions running in parallel both of which need to access the same GPIO pins (they need to turn on/off the same pumps). 我正在使用raspberry pi开发一个项目,在该项目中我需要并行运行两个功能,这两个功能都需要访问相同的GPIO引脚(它们需要打开/关闭相同的泵)。

The issue is that function1 needs to access these pumps every 40 minutes and use them for 5 mins at a time and function2 needs to access them every 3 hours for 5 mins at time. 问题在于,function1需要每40分钟访问一次这些泵,并且一次使用5分钟,而function2需要每3小时一次访问一次,持续5分钟。 The way I am keeping the pumps active is by turning on the GPIO pin, using time.sleep(), then turn it off. 我保持泵工作的方式是使用time.sleep()打开GPIO引脚,然后将其关闭。 After the functions use the pumps they will need to dispense chemicals in water and wait for these chemicals to dissolve (function1 waits 30 minutes, function2 waits 3 hours). 功能使用泵后,他们将需要在水中分配化学药品并等待这些化学药品溶解(功能1等待30分钟,功能2等待3小时)。

I am looking for the best way to run these functions in parallel while taking into account the possible scheduling/timing conflicts between them. 我正在寻找并行运行这些功能的最佳方法,同时考虑到它们之间可能存在的调度/定时冲突。 I want function1 to be able to utilize the pumps even while function2 is waiting for its chemicals to dissolve. 我希望功能1即使功能2正在等待其化学物质溶解时也能够利用泵。 I am thinking to use a global variable to check whether the pump is in use to let the functions know that they need to wait to access those pumps but after some testing I am not sure this works with multiprocessing. 我正在考虑使用全局变量来检查是否正在使用泵,以使函数知道它们需要等待访问这些泵,但是在进行一些测试后,我不确定这是否适用于多处理。

I've set up some test code to simulate the timing of the functions. 我已经设置了一些测试代码来模拟功能的时序。 According to my output, it seems both function1 and function2 are proceeding to dispense their chemicals at the same time. 根据我的输出,功能1和功能2似乎都在同时分配其化学品。 Any ideas or advice is greatly appreciated. 任何想法或建议,我们将不胜感激。

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

You're looking thread (or process) synchronization primitives , in this case, probably a Lock . 您正在寻找线程(或进程) 同步原语 ,在这种情况下,可能是Lock function1 would acquire a blocking lock and release it when it was done. function1将获取一个阻塞锁,并在完成后将其释放。 When function2 attempted to acquire the lock, it would automatically wait until the lock was released. function2尝试获取锁时,它将自动等待直到锁被释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM