Python多处理将命令传递给进程/池/队列?

时间:2015-06-06 19:05:45

标签: python python-multiprocessing

不幸的是,我试图同时与大约14个覆盆子pi进行交流(艺术项目,烧人,不要问......)并且正在使用Paramiko通过SSH连接到RPi,然后发出各种其他命令:同步文件,启动服务器,等等......

要做到这一点,我一直在python中使用多处理模块,但我遇到了错误。在我连接到各种RPis之后,我想让python脚本挂起并等待输入,例如:start server(传递服务器名称,位置等),这将通过Paramiko发送ssh命令以开始运行每个RPI上的python脚本。

我的问题是:如何确保将相应的命令发送到正确的进程/池?例如,如果我实例化连接到各种RPis的类,然后发出启动服务器命令,我想: RPi_A上的服务器用名称A初始化, RPi_B上的服务器用名称B初始化, 而不是名为B的RPi_A,等等......

我是否需要使用process命令?或者泳池会起作用吗?如果是这样,则apply_async,map,map_async。遗憾的是,文档有点模糊。

示例代码:

import sys
import time
import paramiko
import multiprocessing as mp

login = 'pi'
password = 'raspberry'
serverIp = '192.168.1.143'

config = [
    {'hostname': 'pi1.local', 'name': 'carousel'},
    {'hostname': 'pi2.local', 'name': 'bench'}
]



class Dreamlandia():
    def __init__(self):
        pool = mp.Pool(processes=12)
        results = [pool.apply_async(self.connectToServer, args=(dreamlandObject,)) for dreamlandObject in config]
        output = [p.get() for p in results]

    def connectToServer(self, dreamlandObject):
        host = dreamlandObject['hostname']
        structureName = dreamlandObject['name']
        i = 1
        while True:
            try:
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(host, username=login, password=password)
                print ("Connected to " + structureName)
                break
            except paramiko.AuthenticationException:
                print ("Authentication failed when connecting to " + structureName)
                sys.exit(1)
            except:
                print ("Could not SSH to " + structureName + ", waiting for it to start")
                i += 1
                time.sleep(1)

            # If we could not connect within time limit
            if i == 30:
                print ("Could not connect to " + structureName + ". Giving up")
                sys.exit(1)

1 个答案:

答案 0 :(得分:1)

您的主要流程将负责创建,管理和终止您的子流程。

from threading import Thread
from Queue import Queue


def worker(name, queue):
    # connect() #connect to raspPI & stuff ...
    # while True: # in real example this loop ...
        cmd = queue.get()
        print "MSG: thread_%s %s\n" % (name, cmd)
        # execute(cmd) # send command to raspPI
        queue.task_done()

# spawn threads
queues = []
num_threads=4
for i in range(num_threads):
    q = Queue()
    queues.append(q)
    t = Thread(target=worker, args=(i,q))
    t.start()

# send message to every threads
for q in queues:
    q.put("hello", False)

由于我的工作人员结束,我没有杀死我的子进程。你可能想为你的进程添加一个条件,否则你将不得不手动杀死它。