Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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 为什么赢了';t函数并行运行?_Python_Linux_Raspberry Pi_Gpio - Fatal编程技术网

Python 为什么赢了';t函数并行运行?

Python 为什么赢了';t函数并行运行?,python,linux,raspberry-pi,gpio,Python,Linux,Raspberry Pi,Gpio,我一直在阅读并尝试在我的程序中实现多线程,但无论我如何做,它都不会并行运行我的函数。我使用传感器来制作树莓皮3,试图让它们并行打印状态,而不是等到一个完成后再转到下一个函数 现在发生的是,它等待这20秒,然后程序检查秒传感器并打印出状态消息。我不知道为什么 代码: 我不知道为什么您的示例不起作用,但我尝试了以下方法: import time from threading import Thread ''' Define pins and setup the sensor

我一直在阅读并尝试在我的程序中实现多线程,但无论我如何做,它都不会并行运行我的函数。我使用传感器来制作树莓皮3,试图让它们并行打印状态,而不是等到一个完成后再转到下一个函数

现在发生的是,它等待这20秒,然后程序检查秒传感器并打印出状态消息。我不知道为什么

代码:


我不知道为什么您的示例不起作用,但我尝试了以下方法:

    import time
    from threading import Thread

    ''' Define pins and setup the sensors '''
    status = 0

    def runInParallel(*fns):
        proc = []
        for fn in fns:
            p = Thread(target=fn)
            proc.append(p)
        for p in proc:
            p.start()

    def sensor1():
        #Sleep timer long so I can check that I can see prints from 2nd sensor while this thread is sleeping
        time.sleep(.2)
    #Get status from sensor---
        if status == 1:
            print("Ouch!")
        else:
            print("Good!")



    def sensor2():
        time.sleep(0.2)
    #Get status from 2nd sensor---
        if status == 1:
            print("Ouch2!")
        else:
            print("Good2!")

    runInParallel(sensor1, sensor2)
它几乎同时输出
good2
good
。如果你真的需要精确的输出,那么试着调试你的例子,但是如果你用肉眼看不到的距离是可以的,那么我认为线程模块可以很好地工作

编辑:
好的,我认为您的问题在于您认为
进程.join()
计算函数中的等待时间
Process.join()
只确保函数同时启动。如果您在一个函数中有一个等待,那么并行运行
runInParallel
将不关心这一点。

在Windows上运行得很好它是否可以近距离打印“Good!”和“Good2!”?对我来说,在检查Raspian上的下一个函数之前,它需要等待20秒。它打印的是
Good2大约20秒后,它打印的<代码>很好Lol甚至没有读到你的问题。。。抱歉,我认为它是“同时”输出的,因为这两个功能的睡眠时间都是0.2秒。我对第一个函数做了20秒,以明确这两个函数是否可以并行执行。他们没有为我:(你为什么要做20秒?你的
并行运行
函数不计算其他两个函数中的睡眠。它同时执行传感器1和传感器2,但如果一个有等待,另一个没有,那么这些等待将不会被等待。
    import time
    from threading import Thread

    ''' Define pins and setup the sensors '''
    status = 0

    def runInParallel(*fns):
        proc = []
        for fn in fns:
            p = Thread(target=fn)
            proc.append(p)
        for p in proc:
            p.start()

    def sensor1():
        #Sleep timer long so I can check that I can see prints from 2nd sensor while this thread is sleeping
        time.sleep(.2)
    #Get status from sensor---
        if status == 1:
            print("Ouch!")
        else:
            print("Good!")



    def sensor2():
        time.sleep(0.2)
    #Get status from 2nd sensor---
        if status == 1:
            print("Ouch2!")
        else:
            print("Good2!")

    runInParallel(sensor1, sensor2)