进程数量不确定的多处理

时间:2018-10-27 11:39:39

标签: python python-2.7 multiprocessing

我距离适应python还差得很远,因为三天以来,我一直在努力弄清楚如何正确使用多处理程序,但是现在我死了,需要一些帮助。

基本上该程序应该做的是同时从多个(高级随机)输入中控制LED灯带的不同段。因此,我得出的结论是我可能需要使用多重处理。

我已经使用Adafruit的现有模块为其编写了一个模块。 (我将其剥离下来进行演示)

import time
import RPi.GPIO as GPIO
from multiprocessing import Lock
import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI

class Pixels(object):
    def __init__(self, pixelCount, spiPort, spiDevice):
        self.l = Lock()

        self.pixels = Adafruit_WS2801.WS2801Pixels(pixelCount, spi=SPI.SpiDev(spiPort, spiDevice), gpio=GPIO)

        # Clear all the pixels to turn them off.
        self.pixels.clear()
        self.pixels.show() 

    def set_color(self, target_pixel, color=(255,0,0)):
        for k in target_pixel:
            self.l.acquire()
            self.pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2] ))
            self.l.release()
        self.l.acquire()
        self.pixels.show()
        self.l.release()

    def blink_color_blank(self, target_pixel, blink_times=1, wait=0.5, color=(255,0,0)):
        for i in range(blink_times):
            self.set_color(target_pixel, color)
            time.sleep(wait)
            self.set_color(target_pixel, (0,0,0))
            time.sleep(wait)

除了self.pixels以外,还存储了有关哪个LED应该具有哪种颜色的所有信息。 self.pixels.set_pixel()将新值写入存储。 self.pixels.show()实际上将这些值发送到SPI总线。

现在,我开始进行多处理的尝试是这样的。

from multiprocessing import Process, Manager
from multiprocessing.managers import BaseManager
import LED_WS2801

if __name__ == '__main__':
    BaseManager.register('LedClass', LED_WS2801.Pixels)
    manager = BaseManager()
    manager.start()
    inst = manager.LedClass(10,0,0)

现在,当我启动另一个进程仍处于活动状态的进程时,就会出现问题。

    p = Process(target=inst.blink_color_blank, args=([6,7,8], 10, 0.25, (255,0,0),))
    p.start()
    p = Process(target=inst.set_color, args=([3,4,5,6],(0,255,0),))
    p.start()
    p.join()

这给了我以下错误:

Process Process-3:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
File "<string>", line 2, in blink_color_blank
File "/usr/lib/python2.7/multiprocessing/managers.py", line 759, in _callmethod
    kind, result = conn.recv()
EOFError

但是当我这样做时,一切都很好。

    p = Process(target=inst.blink_color_blank, args=([6,7,8], 10, 0.25, (255,0,0),))
    p.start()
    b = Process(target=inst.set_color, args=([3,4,5,6],(0,255,0),))
    b.start()
    p.join()
    b.join()

但是我不知道最终的进程数,因为它们是由外部输入生成的,因此我需要某种方法来控制可变数量的进程。我的想法是使用这样的列表:

    jobs = []

    jobs.append(Process(target=inst.set_color, args=([0,1,2],(255,0,255),)))
    jobs[0].start()

但令我失望的是,这又返回了另一个错误:

    Process Process-2:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
File "<string>", line 2, in set_color
File "/usr/lib/python2.7/multiprocessing/managers.py", line 755, in _callmethod
    self._connect()
File "/usr/lib/python2.7/multiprocessing/managers.py", line 742, in _connect
    conn = self._Client(self._token.address, authkey=self._authkey)
File "/usr/lib/python2.7/multiprocessing/connection.py", line 169, in Client
    c = SocketClient(address)
File "/usr/lib/python2.7/multiprocessing/connection.py", line 308, in SocketClient
    s.connect(address)
File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 2] No such file or directory

我希望我的问题尽可能容易理解和明确。因为我还没有找到这样的东西,所以我想我做的是根本上错误的事情。那你能帮我吗? 谢谢。

1 个答案:

答案 0 :(得分:3)

您必须等待所有子进程完成其工作,然后重新分配p

p = Process(...)
p.start()
p = Process(...)
p.start()
p.join()

您只是在等待p中的后一个操作完成,当主服务器想要终止但第一个子进程仍在运行时,就会出现错误。试试这个以等待所有孩子完成:

p1 = Process(target=inst.blink_color_blank, args=([6,7,8], 10, 0.25, (255,0,0),))
p1.start()
p2 = Process(target=inst.set_color, args=([3,4,5,6],(0,255,0),))
p2.start()
childs = [p1, p2]
while any(p.is_alive() for p in childs):
    for p in childs:
        p.join(1)

此外,有一个multiprocessing.active_children() api可以获取当前进程的所有子进程,以防万一您真的不能从头开始收集列表。