使用多处理

时间:2018-02-16 14:36:12

标签: python python-2.7 raspbian

如何使用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)

0 个答案:

没有答案