简体   繁体   English

Python多处理同时执行更多无限循环

[英]Python multiprocessing more infinite loops at the same time

EDIT: I SOLVED IT! 编辑:我解决了! DON'T MAKE THE SAME MISTAKE AS ME 不要像我一样犯同样的错误

replace this line: 替换此行:

p = multiprocessing.Process(target=check(mval,mname))

with: 有:

p = multiprocessing.Process(target=check, args=(mval,mname))

--------------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------

.

.

.

I am making a robot with Raspberry Pi and some microswitches and I want to check the microswitches if they are triggered so I'm using the multiprocessing module, the process class to be exact with a few infinite while loops and the problem is that one starts and waits until it is triggered and ends and starts the next one instead of all of them starting and running independently. 我正在用Raspberry Pi和一些微动开关制作一个机器人,并且我想检查微动开关是否被触发,所以我使用的是多处理模块,该过程类具有几个无限的while循环,而问题是启动一个并等到它被触发并结束并开始下一个操作,而不是所有其他操作都独立开始和运行。 Here is my code so far. 到目前为止,这是我的代码。

import multiprocessing
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(32, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(7, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(36, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def check(mval, mname):
    while True:
        time.sleep(0.01)
        check = GPIO.input(mval)
        if check == False:  
            print("triggered "  + mname)
            with open("tmp.txt", "w"):
                pass
            f = open("tmp.txt", "w")
            f.write(mname)
            f.close()
            break

def startcheck(mval, mname):
    p = multiprocessing.Process(target=check(mval,mname))
    p.start()
    p.join()


startcheck(32, "m1")
startcheck(7, "m2")
startcheck(12, "m3")
startcheck(36, "m4")

The join() function causes each loop to terminate before the next one starts. join()函数使每个循环在下一个循环开始之前终止。 From the standard library docs: 从标准库文档中:

"join([timeout]) If the optional argument timeout is None (the default), the method blocks until the process whose join() method is called terminates. If timeout is a positive number, it blocks at most timeout seconds. “ join([timeout])如果可选参数timeout为None(默认值),则该方法将阻塞直到调用join()方法的进程终止。如果timeout为正数,则最多阻塞超时秒数。

A process can be joined many times. 一个过程可以多次加入。

A process cannot join itself because this would cause a deadlock. 进程无法加入自身,因为这将导致死锁。 It is an error to attempt to join a process before it has been started." 在启动进程之前尝试加入该进程是错误的。”

Solution: remove the join() line. 解决方案:删除join()行。

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

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