使用多重处理运行多个独立的void函数

2024-05-15 02:35:47 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用python的多处理来运行多个(比如说6个)独立的函数? 我一直在寻找相关的例子,但是我找不到任何一个例子来展示没有参数的函数的例子。是否有任何方法可以通过多重处理来实现如附图所示的功能?我正在寻找一个可以使用和构建的示例结构代码。你知道吗

Intended code structure

编辑:下面是我正在做的一个例子:

import multiprocessing
from multiprocessing import Process
from multiprocessing import Pool
import time
import obd
import RPi.GPIO as GPIO
import time
from ctypes import*
from mcp3208 import MCP3208
from mq import MQ
from smokeSensor import smokePercentage
adc = MCP3208()
GPIO.setmode(GPIO.BCM)
#------Temperature Sensor Module-----------------------
bmp280_lib = CDLL('/home/pi/OBIBB/TempNew/_bmp280_driver.so')
#bmp280_lib = CDLL('/usr/local/lib/_bmp280_driver.so')
bmp280_lib.bmp280GetTempReadings.restype = c_double
bmp280_lib.DisplayAdds.restype = c_void_p
bmp280_lib.bmp280Setup()
################ OBD initialization #########################
ports = obd.scan_serial()       # return list of valid USB or RF ports
print ports                    # ['/dev/ttyUSB0', '/dev/ttyUSB1']
connection = obd.OBD() # auto-connects to USB or RF port


def Read_RPM:
        RESP_RPM = connection.query(cmdrpm)
        return RESP_RPM.value


#cmdrpm = obd.commands.RPM #  OBD command (for reading speed)
def ReadHumidity (Channel):
        # The read_adc function will get the value of the specified channel (0-7).
        readData = adc.read(Channel)
                v_in= (readData*3.3)/4096
                RH=(((v_in/v_supp)-0.1515)/0.00636)
        # Return the ADC values.
        return RH
#################################################
def READ_SENSORS(return_dict):
        v_in = 0
        RH=0
        v_supp=3.3
        HumdityChannel = 0
        T = bmp280_lib.bmp280GetTempReadings()
        Humd = ReadHumidity (HumdityChannel)
        smok = smokePercentage()
        print T
        print Humd
        print smok

def displayAdds():
        os.system("sh kda-chowrangi.sh")

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

if __name__ == '__main__':
        runInParallel(READ_SENSORS)
        pool = Pool(processes=10)
        pool.map_async(displayAdds)

Tags: infromimportgpioreturnlibdefbmp280

热门问题