Python多处理:将变量发送回父级的子级

时间:2014-07-18 18:54:27

标签: python variables multiprocessing parent

我有一个在IP列表上运行函数的池,并返回一个整数(0到99)。子女数量取决于知识产权的数量。这个想法是父建立一个带有IP密钥和整数值的字典。

我需要孩子们将号码报告回父进程,以便将其保存在pickle文件中。将整数变量指定为全局变量无效。

以dict的形式读取一个pickle文件:

infile = open(server_file, 'rb')
servers = pickle.load(infile)
infile.close()

结果:

servers = {'10.116.35.141': ''}

功能:

def check(server):
    response = urllib2.urlopen("http://"+server+"/avicapture.html")
    tall = response.read() # puts the data into a string
    html = tall.rstrip()
    match = re.search('.*In Progress \((.*)%\).*', html)
    if match:
        global temp
        global results
        temp = match.group(1)
        results = temp
        servers[server] = temp  # not setting key to temp in parent process like I want.

游泳池:

p = Pool(len(servers))
p.map(check, servers)
p.close()
p.join()

我在父进程中得到的结果:

servers = {'10.116.35.141': 'integer from child'}

达诺:

现在正在运作。有点。这是当前状态:

从dict开头:

servers = { '10.116.35.141': '' }

def check(server):
    response = urllib2.urlopen("http://"+server+"/avicapture.html")
    tall = response.read() # puts the data into a string
    html = tall.rstrip()
    match = re.search('.*In Progress \((.*)%\).*', html)
    global temp
    if match:
        global temp
        global results
        temp = match.group(1)
        results = temp
        servers[server] = temp
        print servers
    return str(temp)
    print("Results: " +results)

这是汇集:

            if __name__ == "__main__":
                print "Print Servers:\n"+str(servers)
                print "Starting pool"
                p = Pool(len(servers))
                print "p.map(check, servers)"
                results = p.map(check, servers)
                p.close()
                p.join()
            d = dict(ent for ent in zip(servers, results))
            print "d = dict(ent for ent in zip(servers, results))\nResults in.."
            print(d)
            servers = d
            print "Translation into servers dict:"
            print(servers)

这导致了所需的:

Print Servers:
{'10.116.35.141': ''}
Starting pool
p.map(check, servers)
{'10.116.35.141': '96'}
Server: 10.116.35.141 ... 96% 
d = dict(ent for ent in zip(servers, results))
Results in..
{'10.116.35.141': '96'}
Translation into servers dict:
{'10.116.35.141': '96'}

当我有一个带有多个键的字典时,问题就开始了。例如:

servers = { '10.116.35.141': '', '10.116.35.1': '' }

添加附加键的方式是:

        input = raw_input("Add IP: ")
        if input in servers:
            print "Server already in servers list."
        elif input == "":
            print "No input."
        else:
            global servers
            servers[input] = ""

我收到StringIO错误:

Print Servers:
{'192.168.91.236': '', '10.116.35.141': ''}
Starting pool
p.map(check, servers)
Process PoolWorker-1:
Traceback (most recent call last):
  File "/usr/lib64/python2.6/multiprocessing/process.py", line 232, in _bootstrap
    self.run()
  File "/usr/lib64/python2.6/multiprocessing/process.py", line 88, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib64/python2.6/multiprocessing/pool.py", line 71, in worker
    put((job, i, result))
  File "/usr/lib64/python2.6/multiprocessing/queues.py", line 366, in put
    return send(obj)
PicklingError: Can't pickle <type 'cStringIO.StringO'>: attribute lookup cStringIO.StringO failed
{'192.168.91.236': '', '10.116.35.141': '1'}
Traceback (most recent call last):
  File "./bsdae", line 233, in <module>
    results = p.map(check, servers)
  File "/usr/lib64/python2.6/multiprocessing/pool.py", line 148, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/usr/lib64/python2.6/multiprocessing/pool.py", line 422, in get
    raise self._value
UnboundLocalError: local variable 'message' referenced before assignment

1 个答案:

答案 0 :(得分:2)

pool.map实际上允许您直接返回结果:

def check(val):
    return val + 5

if __name__ == "__main__":
    servers = [1,3,4,5]
    p = Pool(len(servers))
    results = p.map(check, servers)
    p.close()
    p.join()
    print(results)

输出:

[6, 8, 9, 10]

您可以将check功能return设为结果,并且最终会显示在map来电返回的列表中。

所以如果你有:

servers = ['1.1.1.1', '2.2.2.2', '3.3.3.3']
results = p.map(check, servers)  # Let's say this returns [1,2,3]

你可以像这样得到你想要的词典:

d = dict(ent for ent in zip(servers, results))
print(d)

输出:

{'3.3.3.3': 3, '1.1.1.1': 1, '2.2.2.2': 2}