Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多处理两个都使用time.sleep()的函数_Python_Multithreading_Raspberry Pi_Multiprocessing_Python Multiprocessing - Fatal编程技术网

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

Python 多处理两个都使用time.sleep()的函数,python,multithreading,raspberry-pi,multiprocessing,python-multiprocessing,Python,Multithreading,Raspberry Pi,Multiprocessing,Python Multiprocessing,我正在与raspberry pi合作的一个项目中,我需要有两个并行运行的函数,这两个函数都需要访问相同的GPIO引脚(它们需要打开/关闭相同的泵) 问题是功能1需要每40分钟访问一次这些泵,每次使用5分钟,功能2需要每3小时访问一次,每次使用5分钟。保持泵处于活动状态的方法是使用time.sleep()打开GPIO引脚,然后将其关闭。功能使用泵后,他们需要在水中分配化学品,并等待这些化学品溶解(功能1等待30分钟,功能2等待3小时) 我正在寻找并行运行这些函数的最佳方法,同时考虑它们之间可能存在

我正在与raspberry pi合作的一个项目中,我需要有两个并行运行的函数,这两个函数都需要访问相同的GPIO引脚(它们需要打开/关闭相同的泵)

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

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

我已经设置了一些测试代码来模拟函数的计时。根据我的输出,功能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()

您正在查看线程(或进程),在本例中,可能是一个
锁<代码>功能1
将获取一个阻塞锁,并在完成时将其释放。当
function2
试图获取锁时,它会自动等待,直到锁被释放。

使用这样的变量在线程之间进行通信可能非常危险。您应该研究如何在线程之间使用适当的线程安全通信方式:。在你的情况下,“锁”可能很有用。是的!使用锁正是我所需要的。谢谢!